1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2003 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 # define EXPORT_SYMTAB
25 #define DEBUG_SUBSYSTEM S_LDLM
28 # include <linux/module.h>
30 # include <liblustre.h>
32 #include <linux/obd.h>
33 #include <linux/obd_ost.h> /* for LUSTRE_OSC_NAME */
34 #include <linux/lustre_mds.h> /* for LUSTRE_MDC_NAME */
35 #include <linux/lustre_mgmt.h>
36 #include <linux/lustre_dlm.h>
37 #include <linux/lustre_net.h>
38 #include <linux/lustre_sec.h>
39 #include <linux/lustre_gs.h>
41 /* @priority: if non-zero, move the selected to the list head
42 * @nocreate: if non-zero, only search in existed connections
44 static int import_set_conn(struct obd_import *imp, struct obd_uuid *uuid,
45 int priority, int nocreate)
47 struct ptlrpc_connection *ptlrpc_conn;
48 struct obd_import_conn *imp_conn = NULL, *item;
52 LASSERT(!(nocreate && !priority));
54 ptlrpc_conn = ptlrpc_uuid_to_connection(uuid);
56 CERROR("can't find connection %s\n", uuid->uuid);
61 OBD_ALLOC(imp_conn, sizeof(*imp_conn));
63 CERROR("fail to alloc memory\n");
64 GOTO(out_put, rc = -ENOMEM);
68 spin_lock(&imp->imp_lock);
69 list_for_each_entry(item, &imp->imp_conn_list, oic_item) {
70 if (obd_uuid_equals(uuid, &item->oic_uuid)) {
72 list_del(&item->oic_item);
73 list_add(&item->oic_item, &imp->imp_conn_list);
74 item->oic_last_attempt = 0;
76 CDEBUG(D_HA, "imp %p@%s: find existed conn %s%s\n",
77 imp, imp->imp_obd->obd_name, uuid->uuid,
78 (priority ? ", move to head." : ""));
79 spin_unlock(&imp->imp_lock);
80 GOTO(out_free, rc = 0);
85 imp_conn->oic_conn = ptlrpc_conn;
86 imp_conn->oic_uuid = *uuid;
87 imp_conn->oic_last_attempt = 0;
89 list_add(&imp_conn->oic_item, &imp->imp_conn_list);
91 list_add_tail(&imp_conn->oic_item, &imp->imp_conn_list);
92 CDEBUG(D_HA, "imp %p@%s: add connection %s at %s\n",
93 imp, imp->imp_obd->obd_name, uuid->uuid,
94 (priority ? "head" : "tail"));
98 spin_unlock(&imp->imp_lock);
102 OBD_FREE(imp_conn, sizeof(*imp_conn));
104 ptlrpc_put_connection(ptlrpc_conn);
108 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid)
110 return import_set_conn(imp, uuid, 1, 1);
113 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
116 return import_set_conn(imp, uuid, priority, 0);
119 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
121 struct obd_import_conn *imp_conn;
122 struct obd_export *dlmexp;
126 spin_lock(&imp->imp_lock);
127 if (list_empty(&imp->imp_conn_list)) {
128 LASSERT(!imp->imp_conn_current);
129 LASSERT(!imp->imp_connection);
133 list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
134 if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
136 LASSERT(imp_conn->oic_conn);
138 /* is current conn? */
139 if (imp_conn == imp->imp_conn_current) {
140 LASSERT(imp_conn->oic_conn == imp->imp_connection);
142 if (imp->imp_state != LUSTRE_IMP_CLOSED &&
143 imp->imp_state != LUSTRE_IMP_DISCON) {
144 CERROR("can't remove current connection\n");
145 GOTO(out, rc = -EBUSY);
148 ptlrpc_put_connection(imp->imp_connection);
149 imp->imp_connection = NULL;
151 dlmexp = class_conn2export(&imp->imp_dlm_handle);
152 if (dlmexp && dlmexp->exp_connection) {
153 LASSERT(dlmexp->exp_connection ==
155 ptlrpc_put_connection(dlmexp->exp_connection);
156 dlmexp->exp_connection = NULL;
160 list_del(&imp_conn->oic_item);
161 ptlrpc_put_connection(imp_conn->oic_conn);
162 OBD_FREE(imp_conn, sizeof(*imp_conn));
163 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
164 imp, imp->imp_obd->obd_name, uuid->uuid);
169 spin_unlock(&imp->imp_lock);
171 CERROR("connection %s not found\n", uuid->uuid);
175 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
177 struct lustre_cfg* lcfg = buf;
178 struct client_obd *cli = &obddev->u.cli;
179 struct obd_import *imp;
180 struct obd_uuid server_uuid;
181 int rq_portal, rp_portal, connect_op;
182 char *name = obddev->obd_type->typ_name;
183 char *mgmt_name = NULL;
187 /* In a more perfect world, we would hang a ptlrpc_client off of
188 * obd_type and just use the values from there. */
189 if (!strcmp(name, OBD_OSC_DEVICENAME)) {
190 rq_portal = OST_REQUEST_PORTAL;
191 rp_portal = OSC_REPLY_PORTAL;
192 connect_op = OST_CONNECT;
193 } else if (!strcmp(name, OBD_MDC_DEVICENAME)) {
194 rq_portal = MDS_REQUEST_PORTAL;
195 rp_portal = MDC_REPLY_PORTAL;
196 connect_op = MDS_CONNECT;
197 } else if (!strcmp(name, OBD_MGMTCLI_DEVICENAME)) {
198 rq_portal = MGMT_REQUEST_PORTAL;
199 rp_portal = MGMT_REPLY_PORTAL;
200 connect_op = MGMT_CONNECT;
201 } else if (!strcmp(name, LUSTRE_GKC_NAME)) {
202 rq_portal = GKS_REQUEST_PORTAL;
203 rp_portal = GKC_REPLY_PORTAL;
204 connect_op = GKS_CONNECT;
207 CERROR("unknown client OBD type \"%s\", can't setup\n",
213 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
214 CERROR("requires a TARGET UUID\n");
218 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
219 CERROR("client UUID must be less than 38 characters\n");
223 if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
224 CERROR("setup requires a SERVER UUID\n");
228 if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
229 CERROR("target UUID must be less than 38 characters\n");
233 sema_init(&cli->cl_sem, 1);
234 cli->cl_conn_count = 0;
235 memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
236 min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
237 sizeof(server_uuid)));
240 cli->cl_avail_grant = 0;
242 /* FIXME: should limit this for the sum of all cl_dirty_max */
243 cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
244 if (cli->cl_dirty_max >> PAGE_SHIFT > num_physpages / 8)
245 cli->cl_dirty_max = num_physpages << (PAGE_SHIFT - 3);
247 INIT_LIST_HEAD(&cli->cl_cache_waiters);
248 INIT_LIST_HEAD(&cli->cl_loi_ready_list);
249 INIT_LIST_HEAD(&cli->cl_loi_write_list);
250 INIT_LIST_HEAD(&cli->cl_loi_read_list);
251 spin_lock_init(&cli->cl_loi_list_lock);
252 cli->cl_r_in_flight = 0;
253 cli->cl_w_in_flight = 0;
254 spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
255 spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
256 spin_lock_init(&cli->cl_read_page_hist.oh_lock);
257 spin_lock_init(&cli->cl_write_page_hist.oh_lock);
259 cli->cl_dstr_in_flight = 0;
260 cli->cl_max_dstr_in_flight = OST_MAX_THREADS;
261 init_waitqueue_head(&cli->cl_wait_for_destroy_slot);
263 memset(&cli->cl_last_write_time, 0,
264 sizeof(cli->cl_last_write_time));
266 cli->cl_cache_wait_num = 0;
267 cli->cl_cache_wait_sum = 0;
268 cli->cl_write_gap_sum = 0;
269 cli->cl_write_gaps = 0;
270 cli->cl_write_num = 0;
271 cli->cl_read_num = 0;
273 cli->cl_dirty_num = 0;
274 cli->cl_dirty_sum = 0;
275 cli->cl_dirty_av = 0;
276 cli->cl_sync_rpcs = 0;
277 cli->cl_dirty_dmax = 0;
278 cli->cl_dirty_dmin = 0;
280 if (num_physpages >> (20 - PAGE_SHIFT) <= 128) { /* <= 128 MB */
281 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 4;
282 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 4;
284 } else if (num_physpages >> (20 - PAGE_SHIFT) <= 512) { /* <= 512 MB */
285 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 2;
286 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 2;
289 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES;
290 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
295 CERROR("ldlm_get_ref failed: %d\n", rc);
299 ptlrpc_init_client(rq_portal, rp_portal, name,
300 &obddev->obd_ldlm_client);
302 imp = class_new_import();
304 GOTO(err_ldlm, rc = -ENOENT);
305 imp->imp_client = &obddev->obd_ldlm_client;
306 imp->imp_obd = obddev;
307 imp->imp_connect_op = connect_op;
308 imp->imp_generation = 0;
309 imp->imp_initial_recov = 1;
310 INIT_LIST_HEAD(&imp->imp_pinger_chain);
311 memcpy(imp->imp_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
312 LUSTRE_CFG_BUFLEN(lcfg, 1));
313 class_import_put(imp);
315 rc = client_import_add_conn(imp, &server_uuid, 1);
317 CERROR("can't add initial connection\n");
318 GOTO(err_import, rc);
321 cli->cl_import = imp;
322 cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
323 cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
324 cli->cl_sandev = to_kdev_t(0);
326 if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
327 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
328 CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
329 name, obddev->obd_name,
330 imp->imp_target_uuid.uuid);
331 imp->imp_invalid = 1;
333 if (LUSTRE_CFG_BUFLEN(lcfg, 4) > 0)
334 mgmt_name = lustre_cfg_string(lcfg, 4);
336 mgmt_name = lustre_cfg_string(lcfg, 3);
340 if (mgmt_name != NULL) {
341 /* Register with management client if we need to. */
342 CDEBUG(D_HA, "%s registering with %s for events about %s\n",
343 obddev->obd_name, mgmt_name, server_uuid.uuid);
345 mgmt_obd = class_name2obd(mgmt_name);
347 CERROR("can't find mgmtcli %s to register\n",
349 GOTO(err_import, rc = -ENOSYS);
352 register_f = (mgmtcli_register_for_events_t)symbol_get("mgmtcli_register_for_events");
354 CERROR("can't i_m_g mgmtcli_register_for_events\n");
355 GOTO(err_import, rc = -ENOSYS);
358 rc = register_f(mgmt_obd, obddev, &imp->imp_target_uuid);
359 symbol_put("mgmtcli_register_for_events");
362 cli->cl_mgmtcli_obd = mgmt_obd;
368 class_destroy_import(imp);
376 int client_obd_cleanup(struct obd_device *obddev, int flags)
378 struct client_obd *cli = &obddev->u.cli;
384 if (cli->cl_mgmtcli_obd) {
385 mgmtcli_deregister_for_events_t dereg_f;
387 dereg_f = (mgmtcli_deregister_for_events_t)symbol_get("mgmtcli_deregister_for_events");
388 dereg_f(cli->cl_mgmtcli_obd, obddev);
389 symbol_put("mgmtcli_deregister_for_events");
392 /* Here we try to drop the security structure after destroy import,
393 * to avoid issue of "sleep in spinlock".
395 class_import_get(cli->cl_import);
396 class_destroy_import(cli->cl_import);
397 ptlrpcs_import_drop_sec(cli->cl_import);
398 class_import_put(cli->cl_import);
399 cli->cl_import = NULL;
401 ldlm_put_ref(flags & OBD_OPT_FORCE);
405 int client_connect_import(struct lustre_handle *dlm_handle,
406 struct obd_device *obd,
407 struct obd_uuid *cluuid,
408 struct obd_connect_data *conn_data,
409 unsigned long connect_flags)
411 struct client_obd *cli = &obd->u.cli;
412 struct obd_import *imp = cli->cl_import;
413 struct obd_export *exp;
418 rc = class_connect(dlm_handle, obd, cluuid);
422 cli->cl_conn_count++;
423 if (cli->cl_conn_count > 1)
425 exp = class_conn2export(dlm_handle);
427 if (obd->obd_namespace != NULL)
428 CERROR("already have namespace!\n");
429 obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
430 LDLM_NAMESPACE_CLIENT);
431 if (obd->obd_namespace == NULL)
432 GOTO(out_disco, rc = -ENOMEM);
434 rc = ptlrpcs_import_get_sec(imp);
438 imp->imp_dlm_handle = *dlm_handle;
439 rc = ptlrpc_init_import(imp);
443 imp->imp_connect_flags = connect_flags;
445 memcpy(&imp->imp_connect_data, conn_data, sizeof(*conn_data));
447 rc = ptlrpc_connect_import(imp, NULL);
449 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
452 LASSERT(exp->exp_connection);
453 ptlrpc_pinger_add_import(imp);
458 ldlm_namespace_free(obd->obd_namespace, 0);
459 obd->obd_namespace = NULL;
461 cli->cl_conn_count--;
462 class_disconnect(exp, 0);
464 class_export_put(exp);
471 int client_disconnect_export(struct obd_export *exp, unsigned long flags)
473 struct obd_device *obd = class_exp2obd(exp);
474 struct client_obd *cli = &obd->u.cli;
475 struct obd_import *imp = cli->cl_import;
480 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
481 exp, exp ? exp->exp_handle.h_cookie : -1);
486 if (!cli->cl_conn_count) {
487 CERROR("disconnecting disconnected device (%s)\n",
489 GOTO(out_sem, rc = -EINVAL);
492 cli->cl_conn_count--;
493 if (cli->cl_conn_count)
494 GOTO(out_no_disconnect, rc = 0);
496 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
497 * delete it regardless. (It's safe to delete an import that was
499 (void)ptlrpc_pinger_del_import(imp);
501 if (obd->obd_namespace != NULL) {
502 /* obd_no_recov == local only */
503 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
504 obd->obd_no_recov, NULL);
505 ldlm_namespace_free(obd->obd_namespace, obd->obd_no_recov);
506 obd->obd_namespace = NULL;
510 * Yeah, obd_no_recov also (mainly) means "forced shutdown".
512 if (obd->obd_no_recov)
513 ptlrpc_invalidate_import(imp, 0);
515 rc = ptlrpc_disconnect_import(imp);
519 err = class_disconnect(exp, 0);
527 /* --------------------------------------------------------------------------
528 * from old lib/target.c
529 * -------------------------------------------------------------------------- */
531 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
532 struct obd_uuid *cluuid, int initial_conn)
534 if (exp->exp_connection && !initial_conn) {
535 struct lustre_handle *hdl;
536 hdl = &exp->exp_imp_reverse->imp_remote_handle;
537 /* Might be a re-connect after a partition. */
538 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
539 CERROR("%s reconnecting\n", cluuid->uuid);
540 conn->cookie = exp->exp_handle.h_cookie;
543 CERROR("%s reconnecting from %s, "
544 "handle mismatch (ours "LPX64", theirs "
545 LPX64")\n", cluuid->uuid,
546 exp->exp_connection->c_remote_uuid.uuid,
547 hdl->cookie, conn->cookie);
548 memset(conn, 0, sizeof *conn);
553 conn->cookie = exp->exp_handle.h_cookie;
554 CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
556 CDEBUG(D_IOCTL,"connect: cookie "LPX64"\n", conn->cookie);
560 static inline int ptlrpc_peer_is_local(struct ptlrpc_peer *peer)
562 ptl_process_id_t myid;
564 PtlGetId(peer->peer_ni->pni_ni_h, &myid);
565 return (memcmp(&peer->peer_id, &myid, sizeof(myid)) == 0);
568 /* To check whether the p_flavor is in deny list or not
571 * EPERM found, refuse
574 static int check_deny_list(struct list_head *head, __u32 flavor)
576 deny_sec_t *p_deny_sec = NULL;
577 deny_sec_t *n_deny_sec = NULL;
579 list_for_each_entry_safe(p_deny_sec, n_deny_sec, head, list) {
580 if (p_deny_sec->flavor == flavor)
586 int target_check_deny_sec(struct obd_device *target, struct ptlrpc_request *req)
591 flavor = req->rq_req_secflvr;
593 if (!strcmp(target->obd_type->typ_name, OBD_MDS_DEVICENAME)) {
594 spin_lock(&target->u.mds.mds_denylist_lock);
595 rc = check_deny_list(&target->u.mds.mds_denylist, flavor);
596 spin_unlock(&target->u.mds.mds_denylist_lock);
597 } else if (!strcmp(target->obd_type->typ_name, OBD_FILTER_DEVICENAME)) {
598 spin_lock(&target->u.filter.fo_denylist_lock);
599 rc = check_deny_list(&target->u.filter.fo_denylist, flavor);
600 spin_unlock(&target->u.filter.fo_denylist_lock);
606 int target_handle_connect(struct ptlrpc_request *req)
608 unsigned long connect_flags = 0, *cfp;
609 struct obd_device *target;
610 struct obd_export *export = NULL;
611 struct obd_import *revimp;
612 struct lustre_handle conn;
613 struct obd_uuid tgtuuid;
614 struct obd_uuid cluuid;
615 struct obd_uuid remote_uuid;
617 struct obd_connect_data *conn_data;
618 int conn_data_size = sizeof(*conn_data);
622 int initial_conn = 0;
623 char peer_str[PTL_NALFMT_SIZE];
624 const int offset = 1;
627 OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
629 LASSERT_REQSWAB (req, offset + 0);
630 str = lustre_msg_string(req->rq_reqmsg, offset + 0,
631 sizeof(tgtuuid) - 1);
633 CERROR("bad target UUID for connect\n");
634 GOTO(out, rc = -EINVAL);
637 obd_str2uuid (&tgtuuid, str);
638 target = class_uuid2obd(&tgtuuid);
640 target = class_name2obd(str);
642 if (!target || target->obd_stopping || !target->obd_set_up) {
643 CERROR("UUID '%s' is not available for connect from %s\n",
644 str, req->rq_peerstr);
645 GOTO(out, rc = -ENODEV);
648 /* check the secure deny list of mds/ost */
649 rc = target_check_deny_sec(target, req);
653 LASSERT_REQSWAB (req, offset + 1);
654 str = lustre_msg_string(req->rq_reqmsg, offset + 1, sizeof(cluuid) - 1);
656 CERROR("bad client UUID for connect\n");
657 GOTO(out, rc = -EINVAL);
660 obd_str2uuid (&cluuid, str);
662 /* XXX extract a nettype and format accordingly */
663 switch (sizeof(ptl_nid_t)) {
664 /* NB the casts only avoid compiler warnings */
666 snprintf((char *)remote_uuid.uuid, sizeof(remote_uuid),
667 "NET_"LPX64"_UUID", (__u64)req->rq_peer.peer_id.nid);
670 snprintf((char *)remote_uuid.uuid, sizeof(remote_uuid),
671 "NET_%x_UUID", (__u32)req->rq_peer.peer_id.nid);
677 tmp = lustre_msg_buf(req->rq_reqmsg, offset + 2, sizeof(conn));
679 GOTO(out, rc = -EPROTO);
681 memcpy(&conn, tmp, sizeof conn);
683 cfp = lustre_msg_buf(req->rq_reqmsg, offset + 3, sizeof(unsigned long));
684 LASSERT(cfp != NULL);
685 connect_flags = *cfp;
687 conn_data = lustre_swab_reqbuf(req, offset + 4, sizeof(*conn_data),
688 lustre_swab_connect);
690 GOTO(out, rc = -EPROTO);
692 rc = lustre_pack_reply(req, 1, &conn_data_size, NULL);
696 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_INITIAL)
699 /* lctl gets a backstage, all-access pass. */
700 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
701 goto dont_check_exports;
703 spin_lock(&target->obd_dev_lock);
704 list_for_each(p, &target->obd_exports) {
705 export = list_entry(p, struct obd_export, exp_obd_chain);
706 if (obd_uuid_equals(&cluuid, &export->exp_client_uuid)) {
707 spin_unlock(&target->obd_dev_lock);
708 LASSERT(export->exp_obd == target);
710 rc = target_handle_reconnect(&conn, export, &cluuid,
716 /* If we found an export, we already unlocked. */
718 spin_unlock(&target->obd_dev_lock);
719 } else if (req->rq_export == NULL &&
720 atomic_read(&export->exp_rpc_count) > 0) {
721 CWARN("%s: refuse connection from %s/%s to 0x%p/%d\n",
722 target->obd_name, cluuid.uuid,
723 ptlrpc_peernid2str(&req->rq_peer, peer_str),
724 export, atomic_read(&export->exp_refcount));
725 GOTO(out, rc = -EBUSY);
726 } else if (req->rq_export != NULL &&
727 atomic_read(&export->exp_rpc_count) > 1) {
728 CWARN("%s: refuse reconnection from %s@%s to 0x%p/%d\n",
729 target->obd_name, cluuid.uuid,
730 ptlrpc_peernid2str(&req->rq_peer, peer_str),
731 export, atomic_read(&export->exp_rpc_count));
732 GOTO(out, rc = -EBUSY);
733 } else if (req->rq_reqmsg->conn_cnt == 1 && !initial_conn) {
734 CERROR("%s reconnected with 1 conn_cnt; cookies not random?\n",
736 GOTO(out, rc = -EALREADY);
739 /* Tell the client if we're in recovery. */
740 /* If this is the first client, start the recovery timer */
741 CWARN("%s: connection from %s@%s/%lu %st"LPU64"\n", target->obd_name,
742 cluuid.uuid, ptlrpc_peernid2str(&req->rq_peer, peer_str), *cfp,
743 target->obd_recovering ? "recovering/" : "", conn_data->transno);
745 if (target->obd_recovering) {
746 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
747 target_start_recovery_timer(target);
751 /* Tell the client if we support replayable requests */
752 if (target->obd_replayable)
753 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
756 if (export == NULL) {
757 if (target->obd_recovering) {
758 CERROR("%s denying connection for new client %s@%s: "
759 "%d clients in recovery for %lds\n", target->obd_name,
761 ptlrpc_peernid2str(&req->rq_peer, peer_str),
762 target->obd_recoverable_clients,
763 (target->obd_recovery_timer.expires-jiffies)/HZ);
767 rc = obd_connect(&conn, target, &cluuid, conn_data,
772 /* Return only the parts of obd_connect_data that we understand, so the
773 * client knows that we don't understand the rest. */
774 conn_data->ocd_connect_flags &= OBD_CONNECT_SUPPORTED;
775 memcpy(lustre_msg_buf(req->rq_repmsg, 0, sizeof(*conn_data)), conn_data,
778 /* Tell the client if we support replayable requests */
779 if (target->obd_replayable)
780 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
782 /* If all else goes well, this is our RPC return code. */
785 if (rc && rc != EALREADY)
788 req->rq_repmsg->handle = conn;
790 /* If the client and the server are the same node, we will already
791 * have an export that really points to the client's DLM export,
792 * because we have a shared handles table.
794 * XXX this will go away when shaver stops sending the "connect" handle
795 * in the real "remote handle" field of the request --phik 24 Apr 2003
797 if (req->rq_export != NULL)
798 class_export_put(req->rq_export);
800 /* ownership of this export ref transfers to the request */
801 export = req->rq_export = class_conn2export(&conn);
802 LASSERT(export != NULL);
804 spin_lock_irqsave(&export->exp_lock, flags);
806 req->rq_repmsg->conn_cnt = export->exp_conn_cnt + 1;
807 } else if (export->exp_conn_cnt >= req->rq_reqmsg->conn_cnt) {
808 CERROR("%s@%s: already connected at a higher conn_cnt: %d > %d\n",
809 cluuid.uuid, ptlrpc_peernid2str(&req->rq_peer, peer_str),
810 export->exp_conn_cnt,
811 req->rq_reqmsg->conn_cnt);
812 spin_unlock_irqrestore(&export->exp_lock, flags);
813 GOTO(out, rc = -EALREADY);
815 export->exp_conn_cnt = req->rq_reqmsg->conn_cnt;
816 spin_unlock_irqrestore(&export->exp_lock, flags);
818 /* request from liblustre? */
819 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT)
820 export->exp_libclient = 1;
822 if (!(lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_ASYNC) &&
823 ptlrpc_peer_is_local(&req->rq_peer)) {
824 CWARN("%s: exp %p set sync\n", target->obd_name, export);
825 export->exp_sync = 1;
827 CDEBUG(D_HA, "%s: exp %p set async\n",target->obd_name,export);
828 export->exp_sync = 0;
831 if (export->exp_connection != NULL)
832 ptlrpc_put_connection(export->exp_connection);
833 export->exp_connection = ptlrpc_get_connection(&req->rq_peer,
836 if (rc == EALREADY) {
837 /* We indicate the reconnection in a flag, not an error code. */
838 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
842 spin_lock_bh(&target->obd_processing_task_lock);
843 if (target->obd_recovering && export->exp_connected == 0) {
844 __u64 t = conn_data->transno;
845 export->exp_connected = 1;
846 if ((lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_TRANSNO)
847 && t < target->obd_next_recovery_transno)
848 target->obd_next_recovery_transno = t;
849 target->obd_connected_clients++;
850 if (target->obd_connected_clients == target->obd_max_recoverable_clients)
851 wake_up(&target->obd_next_transno_waitq);
853 spin_unlock_bh(&target->obd_processing_task_lock);
855 memcpy(&conn, lustre_msg_buf(req->rq_reqmsg, offset + 2, sizeof(conn)),
858 if (export->exp_imp_reverse != NULL) {
859 /* same logic as client_obd_cleanup */
860 class_import_get(export->exp_imp_reverse);
861 class_destroy_import(export->exp_imp_reverse);
862 ptlrpcs_import_drop_sec(export->exp_imp_reverse);
863 class_import_put(export->exp_imp_reverse);
866 /* for the rest part, we return -ENOTCONN in case of errors
867 * in order to let client initialize connection again.
869 revimp = export->exp_imp_reverse = class_new_import();
871 CERROR("fail to alloc new reverse import.\n");
872 GOTO(out, rc = -ENOTCONN);
875 revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
876 revimp->imp_client = &export->exp_obd->obd_ldlm_client;
877 revimp->imp_remote_handle = conn;
878 revimp->imp_obd = target;
879 revimp->imp_dlm_fake = 1;
880 revimp->imp_state = LUSTRE_IMP_FULL;
882 rc = ptlrpcs_import_get_sec(revimp);
884 CERROR("reverse import can not get sec: %d\n", rc);
885 class_destroy_import(revimp);
886 export->exp_imp_reverse = NULL;
887 GOTO(out, rc = -ENOTCONN);
890 class_import_put(revimp);
892 rc = obd_connect_post(export, initial_conn, connect_flags);
899 int target_handle_disconnect(struct ptlrpc_request *req)
901 struct obd_export *exp;
905 rc = lustre_pack_reply(req, 0, NULL, NULL);
909 /* keep the rq_export around so we can send the reply */
910 exp = class_export_get(req->rq_export);
911 req->rq_status = obd_disconnect(exp, 0);
915 void target_destroy_export(struct obd_export *exp)
917 /* exports created from last_rcvd data, and "fake"
918 exports created by lctl don't have an import */
919 if (exp->exp_imp_reverse != NULL) {
920 ptlrpcs_import_drop_sec(exp->exp_imp_reverse);
921 class_destroy_import(exp->exp_imp_reverse);
924 /* We cancel locks at disconnect time, but this will catch any locks
925 * granted in a race with recovery-induced disconnect. */
926 if (exp->exp_obd->obd_namespace != NULL)
927 ldlm_cancel_locks_for_export(exp);
934 struct ptlrpc_request *
935 ptlrpc_clone_req( struct ptlrpc_request *orig_req)
937 struct ptlrpc_request *copy_req;
938 struct lustre_msg *copy_reqmsg;
940 OBD_ALLOC(copy_req, sizeof *copy_req);
943 OBD_ALLOC(copy_reqmsg, orig_req->rq_reqlen);
945 OBD_FREE(copy_req, sizeof *copy_req);
949 memcpy(copy_req, orig_req, sizeof *copy_req);
950 memcpy(copy_reqmsg, orig_req->rq_reqmsg, orig_req->rq_reqlen);
951 /* the copied req takes over the reply state and security data */
952 orig_req->rq_reply_state = NULL;
953 orig_req->rq_svcsec_data = NULL;
955 copy_req->rq_reqmsg = copy_reqmsg;
956 class_export_get(copy_req->rq_export);
957 INIT_LIST_HEAD(©_req->rq_list);
962 void ptlrpc_free_clone( struct ptlrpc_request *req)
965 svcsec_cleanup_req(req);
967 class_export_put(req->rq_export);
968 list_del(&req->rq_list);
969 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
970 OBD_FREE(req, sizeof *req);
973 static void target_release_saved_req(struct ptlrpc_request *req)
976 svcsec_cleanup_req(req);
978 class_export_put(req->rq_export);
979 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
980 OBD_FREE(req, sizeof *req);
983 static void target_finish_recovery(struct obd_device *obd)
987 ldlm_reprocess_all_ns(obd->obd_namespace);
989 /* when recovery finished, cleanup orphans on mds and ost */
990 if (OBT(obd) && OBP(obd, postrecov)) {
991 rc = OBP(obd, postrecov)(obd);
993 CWARN("%s: all clients recovered, %d MDS "
994 "orphans deleted\n", obd->obd_name, rc);
996 CERROR("postrecov failed %d\n", rc);
999 obd->obd_recovery_end = LTIME_S(CURRENT_TIME);
1003 static void abort_req_replay_queue(struct obd_device *obd)
1005 struct ptlrpc_request *req;
1006 struct list_head *tmp, *n;
1009 list_for_each_safe(tmp, n, &obd->obd_req_replay_queue) {
1010 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1011 list_del(&req->rq_list);
1012 DEBUG_REQ(D_ERROR, req, "aborted:");
1013 req->rq_status = -ENOTCONN;
1014 req->rq_type = PTL_RPC_MSG_ERR;
1015 rc = lustre_pack_reply(req, 0, NULL, NULL);
1019 DEBUG_REQ(D_ERROR, req,
1020 "packing failed for abort-reply; skipping");
1022 target_release_saved_req(req);
1026 static void abort_lock_replay_queue(struct obd_device *obd)
1028 struct ptlrpc_request *req;
1029 struct list_head *tmp, *n;
1032 list_for_each_safe(tmp, n, &obd->obd_lock_replay_queue) {
1033 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1034 list_del(&req->rq_list);
1035 DEBUG_REQ(D_ERROR, req, "aborted:");
1036 req->rq_status = -ENOTCONN;
1037 req->rq_type = PTL_RPC_MSG_ERR;
1038 rc = lustre_pack_reply(req, 0, NULL, NULL);
1042 DEBUG_REQ(D_ERROR, req,
1043 "packing failed for abort-reply; skipping");
1045 target_release_saved_req(req);
1049 /* Called from a cleanup function if the device is being cleaned up
1050 forcefully. The exports should all have been disconnected already,
1051 the only thing left to do is
1052 - clear the recovery flags
1054 - free queued requests and replies, but don't send replies
1055 Because the obd_stopping flag is set, no new requests should be received.
1058 void target_cleanup_recovery(struct obd_device *obd)
1060 struct list_head *tmp, *n;
1061 struct ptlrpc_request *req;
1063 spin_lock_bh(&obd->obd_processing_task_lock);
1064 if (!obd->obd_recovering) {
1065 spin_unlock_bh(&obd->obd_processing_task_lock);
1069 obd->obd_recovering = obd->obd_abort_recovery = 0;
1070 target_cancel_recovery_timer(obd);
1071 spin_unlock_bh(&obd->obd_processing_task_lock);
1073 list_for_each_safe(tmp, n, &obd->obd_req_replay_queue) {
1074 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1075 list_del(&req->rq_list);
1076 LASSERT (req->rq_reply_state == 0);
1077 target_release_saved_req(req);
1079 list_for_each_safe(tmp, n, &obd->obd_lock_replay_queue) {
1080 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1081 list_del(&req->rq_list);
1082 LASSERT (req->rq_reply_state == 0);
1083 target_release_saved_req(req);
1085 list_for_each_safe(tmp, n, &obd->obd_final_req_queue) {
1086 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1087 list_del(&req->rq_list);
1088 LASSERT (req->rq_reply_state == 0);
1089 target_release_saved_req(req);
1094 static void target_abort_recovery(void *data)
1096 struct obd_device *obd = data;
1098 LASSERT(!obd->obd_recovering);
1100 class_disconnect_stale_exports(obd, 0);
1102 CERROR("%s: recovery period over; disconnecting unfinished clients.\n",
1105 abort_recovery_queue(obd);
1106 target_finish_recovery(obd);
1107 ptlrpc_run_recovery_over_upcall(obd);
1111 static void target_recovery_expired(unsigned long castmeharder)
1113 struct obd_device *obd = (struct obd_device *)castmeharder;
1114 spin_lock_bh(&obd->obd_processing_task_lock);
1115 if (obd->obd_recovering)
1116 obd->obd_abort_recovery = 1;
1118 wake_up(&obd->obd_next_transno_waitq);
1119 spin_unlock_bh(&obd->obd_processing_task_lock);
1123 /* obd_processing_task_lock should be held */
1124 void target_cancel_recovery_timer(struct obd_device *obd)
1126 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
1127 del_timer(&obd->obd_recovery_timer);
1131 static void reset_recovery_timer(struct obd_device *obd)
1133 spin_lock_bh(&obd->obd_processing_task_lock);
1134 if (!obd->obd_recovering) {
1135 spin_unlock_bh(&obd->obd_processing_task_lock);
1138 CDEBUG(D_HA, "timer will expire in %u seconds\n",
1139 OBD_RECOVERY_TIMEOUT / HZ);
1140 mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
1141 spin_unlock_bh(&obd->obd_processing_task_lock);
1145 /* Only start it the first time called */
1146 void target_start_recovery_timer(struct obd_device *obd)
1148 spin_lock_bh(&obd->obd_processing_task_lock);
1149 if (!obd->obd_recovering || timer_pending(&obd->obd_recovery_timer)) {
1150 spin_unlock_bh(&obd->obd_processing_task_lock);
1153 CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
1154 OBD_RECOVERY_TIMEOUT / HZ);
1155 obd->obd_recovery_timer.function = target_recovery_expired;
1156 obd->obd_recovery_timer.data = (unsigned long)obd;
1157 mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
1158 spin_unlock_bh(&obd->obd_processing_task_lock);
1162 static int check_for_next_transno(struct obd_device *obd)
1164 struct ptlrpc_request *req = NULL;
1165 int wake_up = 0, connected, completed, queue_len, max;
1166 __u64 next_transno, req_transno;
1168 spin_lock_bh(&obd->obd_processing_task_lock);
1169 if (!list_empty(&obd->obd_req_replay_queue)) {
1170 req = list_entry(obd->obd_req_replay_queue.next,
1171 struct ptlrpc_request, rq_list);
1172 req_transno = req->rq_reqmsg->transno;
1177 max = obd->obd_max_recoverable_clients;
1178 connected = obd->obd_connected_clients;
1179 completed = max - obd->obd_recoverable_clients;
1180 queue_len = obd->obd_requests_queued_for_recovery;
1181 next_transno = obd->obd_next_recovery_transno;
1183 CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
1184 "req_transno: "LPU64", next_transno: "LPU64"\n",
1185 max, connected, completed, queue_len, req_transno, next_transno);
1186 if (obd->obd_abort_recovery) {
1187 CDEBUG(D_HA, "waking for aborted recovery\n");
1189 } else if (atomic_read(&obd->obd_req_replay_clients) == 0) {
1190 CDEBUG(D_HA, "waking for completed recovery\n");
1192 } else if (req_transno == next_transno) {
1193 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1195 } else if (queue_len + completed == max) {
1196 LASSERT(req->rq_reqmsg->transno >= next_transno);
1197 CDEBUG(req_transno > obd->obd_last_committed ? D_ERROR : D_HA,
1198 "waking for skipped transno (skip: "LPD64
1199 ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
1200 next_transno, queue_len, completed, max, req_transno);
1201 obd->obd_next_recovery_transno = req_transno;
1203 } else if (queue_len == atomic_read(&obd->obd_req_replay_clients)) {
1204 /* some clients haven't connected in time, but we can try
1205 * to replay requests that demand on already committed ones
1206 * also, we can replay first non-committed transation */
1207 LASSERT(req_transno != 0);
1208 if (req_transno == obd->obd_last_committed + 1) {
1209 obd->obd_next_recovery_transno = req_transno;
1210 } else if (req_transno > obd->obd_last_committed) {
1211 /* can't continue recovery: have no needed transno */
1212 obd->obd_abort_recovery = 1;
1213 CDEBUG(D_ERROR, "abort due to missed clients. max: %d, "
1214 "connected: %d, completed: %d, queue_len: %d, "
1215 "req_transno: "LPU64", next_transno: "LPU64"\n",
1216 max, connected, completed, queue_len,
1217 req_transno, next_transno);
1221 spin_unlock_bh(&obd->obd_processing_task_lock);
1226 static struct ptlrpc_request *
1227 target_next_replay_req(struct obd_device *obd)
1229 struct l_wait_info lwi = { 0 };
1230 struct ptlrpc_request *req;
1232 CDEBUG(D_HA, "Waiting for transno "LPD64"\n",
1233 obd->obd_next_recovery_transno);
1234 l_wait_event(obd->obd_next_transno_waitq,
1235 check_for_next_transno(obd), &lwi);
1237 spin_lock_bh(&obd->obd_processing_task_lock);
1238 if (obd->obd_abort_recovery) {
1240 } else if (!list_empty(&obd->obd_req_replay_queue)) {
1241 req = list_entry(obd->obd_req_replay_queue.next,
1242 struct ptlrpc_request, rq_list);
1243 list_del_init(&req->rq_list);
1244 obd->obd_requests_queued_for_recovery--;
1248 spin_unlock_bh(&obd->obd_processing_task_lock);
1252 static int check_for_next_lock(struct obd_device *obd)
1254 struct ptlrpc_request *req = NULL;
1257 spin_lock_bh(&obd->obd_processing_task_lock);
1258 if (!list_empty(&obd->obd_lock_replay_queue)) {
1259 req = list_entry(obd->obd_lock_replay_queue.next,
1260 struct ptlrpc_request, rq_list);
1261 CDEBUG(D_HA, "waking for next lock\n");
1263 } else if (atomic_read(&obd->obd_lock_replay_clients) == 0) {
1264 CDEBUG(D_HA, "waking for completed lock replay\n");
1266 } else if (obd->obd_abort_recovery) {
1267 CDEBUG(D_HA, "waking for aborted recovery\n");
1270 spin_unlock_bh(&obd->obd_processing_task_lock);
1275 static struct ptlrpc_request *
1276 target_next_replay_lock(struct obd_device *obd)
1278 struct l_wait_info lwi = { 0 };
1279 struct ptlrpc_request *req;
1281 CDEBUG(D_HA, "Waiting for lock\n");
1282 l_wait_event(obd->obd_next_transno_waitq,
1283 check_for_next_lock(obd), &lwi);
1285 spin_lock_bh(&obd->obd_processing_task_lock);
1286 if (obd->obd_abort_recovery) {
1288 } else if (!list_empty(&obd->obd_lock_replay_queue)) {
1289 req = list_entry(obd->obd_lock_replay_queue.next,
1290 struct ptlrpc_request, rq_list);
1291 list_del_init(&req->rq_list);
1295 spin_unlock_bh(&obd->obd_processing_task_lock);
1299 static struct ptlrpc_request *
1300 target_next_final_ping(struct obd_device *obd)
1302 struct ptlrpc_request *req;
1304 spin_lock_bh(&obd->obd_processing_task_lock);
1305 if (!list_empty(&obd->obd_final_req_queue)) {
1306 req = list_entry(obd->obd_final_req_queue.next,
1307 struct ptlrpc_request, rq_list);
1308 list_del_init(&req->rq_list);
1312 spin_unlock_bh(&obd->obd_processing_task_lock);
1316 static int req_replay_done(struct obd_export *exp)
1318 if (exp->exp_req_replay_needed)
1323 static int lock_replay_done(struct obd_export *exp)
1325 if (exp->exp_lock_replay_needed)
1330 static int connect_done(struct obd_export *exp)
1332 if (exp->exp_connected)
1337 static int check_for_clients(struct obd_device *obd)
1339 if (obd->obd_abort_recovery)
1341 LASSERT(obd->obd_connected_clients <= obd->obd_max_recoverable_clients);
1342 if (obd->obd_connected_clients == obd->obd_max_recoverable_clients)
1347 static int target_recovery_thread(void *arg)
1349 struct obd_device *obd = arg;
1350 struct ptlrpc_request *req;
1351 struct target_recovery_data *trd = &obd->obd_recovery_data;
1352 char peer_str[PTL_NALFMT_SIZE];
1353 struct l_wait_info lwi = { 0 };
1354 unsigned long delta;
1355 unsigned long flags;
1358 kportal_daemonize("tgt-recov");
1360 SIGNAL_MASK_LOCK(current, flags);
1361 sigfillset(¤t->blocked);
1363 SIGNAL_MASK_UNLOCK(current, flags);
1365 CERROR("%s: started recovery thread pid %d\n", obd->obd_name,
1367 trd->trd_processing_task = current->pid;
1369 obd->obd_recovering = 1;
1370 complete(&trd->trd_starting);
1372 /* first of all, we have to know the first transno to replay */
1373 obd->obd_abort_recovery = 0;
1374 l_wait_event(obd->obd_next_transno_waitq,
1375 check_for_clients(obd), &lwi);
1377 spin_lock_bh(&obd->obd_processing_task_lock);
1378 target_cancel_recovery_timer(obd);
1379 spin_unlock_bh(&obd->obd_processing_task_lock);
1381 /* If some clients haven't connected in time, evict them */
1382 if (obd->obd_abort_recovery) {
1384 CDEBUG(D_ERROR, "few clients haven't connect in time (%d/%d),"
1385 "evict them ...\n", obd->obd_connected_clients,
1386 obd->obd_max_recoverable_clients);
1387 obd->obd_abort_recovery = 0;
1388 stale = class_disconnect_stale_exports(obd, connect_done, 0);
1389 atomic_sub(stale, &obd->obd_req_replay_clients);
1390 atomic_sub(stale, &obd->obd_lock_replay_clients);
1393 /* next stage: replay requests */
1395 obd->obd_req_replaying = 1;
1396 CDEBUG(D_ERROR, "1: request replay stage - %d clients from t"LPU64"\n",
1397 atomic_read(&obd->obd_req_replay_clients),
1398 obd->obd_next_recovery_transno);
1399 while ((req = target_next_replay_req(obd))) {
1400 LASSERT(trd->trd_processing_task == current->pid);
1401 DEBUG_REQ(D_HA, req, "processing t"LPD64" from %s",
1402 req->rq_reqmsg->transno,
1403 ptlrpc_peernid2str(&req->rq_peer, peer_str));
1404 (void)trd->trd_recovery_handler(req);
1405 obd->obd_replayed_requests++;
1406 reset_recovery_timer(obd);
1407 /* bug 1580: decide how to properly sync() in recovery*/
1408 //mds_fsync_super(mds->mds_sb);
1409 ptlrpc_free_clone(req);
1410 spin_lock_bh(&obd->obd_processing_task_lock);
1411 obd->obd_next_recovery_transno++;
1412 spin_unlock_bh(&obd->obd_processing_task_lock);
1415 spin_lock_bh(&obd->obd_processing_task_lock);
1416 target_cancel_recovery_timer(obd);
1417 spin_unlock_bh(&obd->obd_processing_task_lock);
1419 /* If some clients haven't replayed requests in time, evict them */
1420 if (obd->obd_abort_recovery) {
1422 CDEBUG(D_ERROR, "req replay timed out, aborting ...\n");
1423 obd->obd_abort_recovery = 0;
1424 stale = class_disconnect_stale_exports(obd, req_replay_done, 0);
1425 atomic_sub(stale, &obd->obd_lock_replay_clients);
1426 abort_req_replay_queue(obd);
1427 /* XXX for debuggin tests 11 and 17 */
1431 /* The second stage: replay locks */
1432 CDEBUG(D_ERROR, "2: lock replay stage - %d clients\n",
1433 atomic_read(&obd->obd_lock_replay_clients));
1434 while ((req = target_next_replay_lock(obd))) {
1435 LASSERT(trd->trd_processing_task == current->pid);
1436 DEBUG_REQ(D_HA, req, "processing lock from %s: ",
1437 ptlrpc_peernid2str(&req->rq_peer, peer_str));
1438 (void)trd->trd_recovery_handler(req);
1439 reset_recovery_timer(obd);
1440 ptlrpc_free_clone(req);
1441 obd->obd_replayed_locks++;
1444 spin_lock_bh(&obd->obd_processing_task_lock);
1445 target_cancel_recovery_timer(obd);
1446 spin_unlock_bh(&obd->obd_processing_task_lock);
1448 /* If some clients haven't replayed requests in time, evict them */
1449 if (obd->obd_abort_recovery) {
1451 CERROR("lock replay timed out, aborting ...\n");
1452 obd->obd_abort_recovery = 0;
1453 stale = class_disconnect_stale_exports(obd, lock_replay_done, 0);
1454 abort_lock_replay_queue(obd);
1457 /* We drop recoverying flag to forward all new requests
1458 * to regular mds_handle() since now */
1459 spin_lock_bh(&obd->obd_processing_task_lock);
1460 obd->obd_recovering = 0;
1461 spin_unlock_bh(&obd->obd_processing_task_lock);
1463 /* The third stage: reply on final pings */
1464 CDEBUG(D_ERROR, "3: final stage - process recovery completion pings\n");
1465 while ((req = target_next_final_ping(obd))) {
1466 LASSERT(trd->trd_processing_task == current->pid);
1467 DEBUG_REQ(D_HA, req, "processing final ping from %s: ",
1468 ptlrpc_peernid2str(&req->rq_peer, peer_str));
1469 (void)trd->trd_recovery_handler(req);
1470 ptlrpc_free_clone(req);
1473 delta = (jiffies - delta) / HZ;
1474 CDEBUG(D_ERROR,"4: recovery completed in %lus - %d/%d reqs/locks\n",
1475 delta, obd->obd_replayed_requests, obd->obd_replayed_locks);
1476 if (delta > obd_timeout * 2) {
1477 CWARN("too long recovery - read logs\n");
1478 portals_debug_dumplog();
1480 target_finish_recovery(obd);
1482 trd->trd_processing_task = 0;
1483 complete(&trd->trd_finishing);
1487 int target_start_recovery_thread(struct obd_device *obd, svc_handler_t handler)
1490 struct target_recovery_data *trd = &obd->obd_recovery_data;
1492 memset(trd, 0, sizeof(*trd));
1493 init_completion(&trd->trd_starting);
1494 init_completion(&trd->trd_finishing);
1495 trd->trd_recovery_handler = handler;
1497 if (kernel_thread(target_recovery_thread, obd, 0) > 0) {
1498 wait_for_completion(&trd->trd_starting);
1499 LASSERT(obd->obd_recovering != 0);
1506 void target_stop_recovery_thread(struct obd_device *obd)
1508 spin_lock_bh(&obd->obd_processing_task_lock);
1509 if (obd->obd_recovery_data.trd_processing_task > 0) {
1510 struct target_recovery_data *trd = &obd->obd_recovery_data;
1511 CERROR("%s: aborting recovery\n", obd->obd_name);
1512 obd->obd_abort_recovery = 1;
1513 wake_up(&obd->obd_next_transno_waitq);
1514 spin_unlock_bh(&obd->obd_processing_task_lock);
1515 wait_for_completion(&trd->trd_finishing);
1517 spin_unlock_bh(&obd->obd_processing_task_lock);
1522 int target_process_req_flags(struct obd_device *obd, struct ptlrpc_request *req)
1524 struct obd_export *exp = req->rq_export;
1525 LASSERT(exp != NULL);
1526 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
1527 /* client declares he's ready to replay locks */
1528 spin_lock_bh(&obd->obd_processing_task_lock);
1529 if (exp->exp_req_replay_needed) {
1530 LASSERT(atomic_read(&obd->obd_req_replay_clients) > 0);
1531 exp->exp_req_replay_needed = 0;
1532 atomic_dec(&obd->obd_req_replay_clients);
1533 obd->obd_recoverable_clients--;
1534 if (atomic_read(&obd->obd_req_replay_clients) == 0)
1535 CDEBUG(D_HA, "all clients have replayed reqs\n");
1536 wake_up(&obd->obd_next_transno_waitq);
1538 spin_unlock_bh(&obd->obd_processing_task_lock);
1540 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
1541 /* client declares he's ready to complete recovery
1542 * so, we put the request on th final queue */
1543 spin_lock_bh(&obd->obd_processing_task_lock);
1544 if (exp->exp_lock_replay_needed) {
1545 LASSERT(atomic_read(&obd->obd_lock_replay_clients) > 0);
1546 exp->exp_lock_replay_needed = 0;
1547 atomic_dec(&obd->obd_lock_replay_clients);
1548 if (atomic_read(&obd->obd_lock_replay_clients) == 0)
1549 CDEBUG(D_HA, "all clients have replayed locks\n");
1550 wake_up(&obd->obd_next_transno_waitq);
1552 spin_unlock_bh(&obd->obd_processing_task_lock);
1558 int target_queue_recovery_request(struct ptlrpc_request *req,
1559 struct obd_device *obd)
1561 struct list_head *tmp;
1563 __u64 transno = req->rq_reqmsg->transno;
1565 if (obd->obd_recovery_data.trd_processing_task == current->pid) {
1566 /* Processing the queue right now, don't re-add. */
1570 target_process_req_flags(obd, req);
1572 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
1573 /* client declares he's ready to complete recovery
1574 * so, we put the request on th final queue */
1575 req = ptlrpc_clone_req(req);
1578 DEBUG_REQ(D_HA, req, "queue final req");
1579 spin_lock_bh(&obd->obd_processing_task_lock);
1580 list_add_tail(&req->rq_list, &obd->obd_final_req_queue);
1581 spin_unlock_bh(&obd->obd_processing_task_lock);
1584 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
1585 /* client declares he's ready to replay locks */
1586 req = ptlrpc_clone_req(req);
1589 DEBUG_REQ(D_HA, req, "queue lock replay req");
1590 spin_lock_bh(&obd->obd_processing_task_lock);
1591 list_add_tail(&req->rq_list, &obd->obd_lock_replay_queue);
1592 spin_unlock_bh(&obd->obd_processing_task_lock);
1593 wake_up(&obd->obd_next_transno_waitq);
1598 /* CAVEAT EMPTOR: The incoming request message has been swabbed
1599 * (i.e. buflens etc are in my own byte order), but type-dependent
1600 * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1603 INIT_LIST_HEAD(&req->rq_list);
1604 DEBUG_REQ(D_HA, req, "not queueing");
1609 /* If we're processing the queue, we want don't want to queue this
1612 * Also, if this request has a transno less than the one we're waiting
1613 * for, we should process it now. It could (and currently always will)
1614 * be an open request for a descriptor that was opened some time ago.
1616 * Also, a resent, replayed request that has already been
1617 * handled will pass through here and be processed immediately.
1619 spin_lock_bh(&obd->obd_processing_task_lock);
1620 if (transno < obd->obd_next_recovery_transno && obd->obd_req_replaying) {
1621 /* Processing the queue right now, don't re-add. */
1622 LASSERT(list_empty(&req->rq_list));
1623 spin_unlock_bh(&obd->obd_processing_task_lock);
1626 spin_unlock_bh(&obd->obd_processing_task_lock);
1628 /* A resent, replayed request that is still on the queue; just drop it.
1629 The queued request will handle this. */
1630 if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT | MSG_REPLAY))
1631 == (MSG_RESENT | MSG_REPLAY)) {
1632 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1636 req = ptlrpc_clone_req(req);
1640 spin_lock_bh(&obd->obd_processing_task_lock);
1643 list_for_each(tmp, &obd->obd_req_replay_queue) {
1644 struct ptlrpc_request *reqiter =
1645 list_entry(tmp, struct ptlrpc_request, rq_list);
1647 if (reqiter->rq_reqmsg->transno > transno) {
1648 list_add_tail(&req->rq_list, &reqiter->rq_list);
1655 list_add_tail(&req->rq_list, &obd->obd_req_replay_queue);
1657 obd->obd_requests_queued_for_recovery++;
1658 wake_up(&obd->obd_next_transno_waitq);
1659 spin_unlock_bh(&obd->obd_processing_task_lock);
1663 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1665 return req->rq_export->exp_obd;
1669 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1671 if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1672 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1673 DEBUG_REQ(D_ERROR, req, "dropping reply");
1674 /* NB this does _not_ send with ACK disabled, to simulate
1675 * sending OK, but timing out for the ACK */
1676 if (req->rq_reply_state != NULL) {
1677 if (!req->rq_reply_state->rs_difficult) {
1678 lustre_free_reply_state (req->rq_reply_state);
1679 req->rq_reply_state = NULL;
1681 struct ptlrpc_service *svc =
1682 req->rq_rqbd->rqbd_srv_ni->sni_service;
1683 atomic_inc(&svc->srv_outstanding_replies);
1690 req->rq_status = rc;
1691 return (ptlrpc_error(req));
1693 DEBUG_REQ(D_NET, req, "sending reply");
1696 return (ptlrpc_send_reply(req, 1));
1700 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1703 unsigned long flags;
1704 struct ptlrpc_reply_state *rs;
1705 struct obd_device *obd;
1706 struct obd_export *exp;
1707 struct ptlrpc_srv_ni *sni;
1708 struct ptlrpc_service *svc;
1710 sni = req->rq_rqbd->rqbd_srv_ni;
1711 svc = sni->sni_service;
1713 rs = req->rq_reply_state;
1714 if (rs == NULL || !rs->rs_difficult) {
1715 /* The easy case; no notifiers and reply_out_callback()
1716 * cleans up (i.e. we can't look inside rs after a
1717 * successful send) */
1718 netrc = target_send_reply_msg (req, rc, fail_id);
1720 LASSERT (netrc == 0 || req->rq_reply_state == NULL);
1724 /* must be an export if locks saved */
1725 LASSERT (req->rq_export != NULL);
1726 /* req/reply consistent */
1727 LASSERT (rs->rs_srv_ni == sni);
1730 LASSERT (!rs->rs_scheduled);
1731 LASSERT (!rs->rs_scheduled_ever);
1732 LASSERT (!rs->rs_handled);
1733 LASSERT (!rs->rs_on_net);
1734 LASSERT (rs->rs_export == NULL);
1735 LASSERT (list_empty(&rs->rs_obd_list));
1736 LASSERT (list_empty(&rs->rs_exp_list));
1738 exp = class_export_get (req->rq_export);
1741 /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1742 rs->rs_scheduled = 1;
1744 rs->rs_xid = req->rq_xid;
1745 rs->rs_transno = req->rq_transno;
1746 rs->rs_export = exp;
1748 spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
1750 if (rs->rs_transno > obd->obd_last_committed) {
1751 /* not committed already */
1752 list_add_tail (&rs->rs_obd_list,
1753 &obd->obd_uncommitted_replies);
1756 spin_unlock (&obd->obd_uncommitted_replies_lock);
1757 spin_lock (&exp->exp_lock);
1759 list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1761 spin_unlock_irqrestore (&exp->exp_lock, flags);
1763 netrc = target_send_reply_msg (req, rc, fail_id);
1765 spin_lock_irqsave (&svc->srv_lock, flags);
1767 svc->srv_n_difficult_replies++;
1769 if (netrc != 0) /* error sending: reply is off the net */
1772 if (!rs->rs_on_net || /* some notifier */
1773 list_empty(&rs->rs_exp_list) || /* completed already */
1774 list_empty(&rs->rs_obd_list)) {
1775 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1776 wake_up (&svc->srv_waitq);
1778 list_add (&rs->rs_list, &sni->sni_active_replies);
1779 rs->rs_scheduled = 0; /* allow notifier to schedule */
1782 spin_unlock_irqrestore (&svc->srv_lock, flags);
1785 int target_handle_ping(struct ptlrpc_request *req)
1787 return lustre_pack_reply(req, 0, NULL, NULL);