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>
40 /* @priority: if non-zero, move the selected to the list head
41 * @nocreate: if non-zero, only search in existed connections
43 static int import_set_conn(struct obd_import *imp, struct obd_uuid *uuid,
44 int priority, int nocreate)
46 struct ptlrpc_connection *ptlrpc_conn;
47 struct obd_import_conn *imp_conn = NULL, *item;
51 LASSERT(!(nocreate && !priority));
53 ptlrpc_conn = ptlrpc_uuid_to_connection(uuid);
55 CERROR("can't find connection %s\n", uuid->uuid);
60 OBD_ALLOC(imp_conn, sizeof(*imp_conn));
62 CERROR("fail to alloc memory\n");
63 GOTO(out_put, rc = -ENOMEM);
67 spin_lock(&imp->imp_lock);
68 list_for_each_entry(item, &imp->imp_conn_list, oic_item) {
69 if (obd_uuid_equals(uuid, &item->oic_uuid)) {
71 list_del(&item->oic_item);
72 list_add(&item->oic_item, &imp->imp_conn_list);
73 item->oic_last_attempt = 0;
75 CDEBUG(D_HA, "imp %p@%s: find existed conn %s%s\n",
76 imp, imp->imp_obd->obd_name, uuid->uuid,
77 (priority ? ", move to head." : ""));
78 spin_unlock(&imp->imp_lock);
79 GOTO(out_free, rc = 0);
84 imp_conn->oic_conn = ptlrpc_conn;
85 imp_conn->oic_uuid = *uuid;
86 imp_conn->oic_last_attempt = 0;
88 list_add(&imp_conn->oic_item, &imp->imp_conn_list);
90 list_add_tail(&imp_conn->oic_item, &imp->imp_conn_list);
91 CDEBUG(D_HA, "imp %p@%s: add connection %s at %s\n",
92 imp, imp->imp_obd->obd_name, uuid->uuid,
93 (priority ? "head" : "tail"));
97 spin_unlock(&imp->imp_lock);
101 OBD_FREE(imp_conn, sizeof(*imp_conn));
103 ptlrpc_put_connection(ptlrpc_conn);
107 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid)
109 return import_set_conn(imp, uuid, 1, 1);
112 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
115 return import_set_conn(imp, uuid, priority, 0);
118 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
120 struct obd_import_conn *imp_conn;
121 struct obd_export *dlmexp;
125 spin_lock(&imp->imp_lock);
126 if (list_empty(&imp->imp_conn_list)) {
127 LASSERT(!imp->imp_conn_current);
128 LASSERT(!imp->imp_connection);
132 list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
133 if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
135 LASSERT(imp_conn->oic_conn);
137 /* is current conn? */
138 if (imp_conn == imp->imp_conn_current) {
139 LASSERT(imp_conn->oic_conn == imp->imp_connection);
141 if (imp->imp_state != LUSTRE_IMP_CLOSED &&
142 imp->imp_state != LUSTRE_IMP_DISCON) {
143 CERROR("can't remove current connection\n");
144 GOTO(out, rc = -EBUSY);
147 ptlrpc_put_connection(imp->imp_connection);
148 imp->imp_connection = NULL;
150 dlmexp = class_conn2export(&imp->imp_dlm_handle);
151 if (dlmexp && dlmexp->exp_connection) {
152 LASSERT(dlmexp->exp_connection ==
154 ptlrpc_put_connection(dlmexp->exp_connection);
155 dlmexp->exp_connection = NULL;
159 list_del(&imp_conn->oic_item);
160 ptlrpc_put_connection(imp_conn->oic_conn);
161 OBD_FREE(imp_conn, sizeof(*imp_conn));
162 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
163 imp, imp->imp_obd->obd_name, uuid->uuid);
168 spin_unlock(&imp->imp_lock);
170 CERROR("connection %s not found\n", uuid->uuid);
174 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
176 struct lustre_cfg* lcfg = buf;
177 struct client_obd *cli = &obddev->u.cli;
178 struct obd_import *imp;
179 struct obd_uuid server_uuid;
180 int rq_portal, rp_portal, connect_op;
181 char *name = obddev->obd_type->typ_name;
182 char *mgmt_name = NULL;
184 struct obd_device *mgmt_obd;
185 mgmtcli_register_for_events_t register_f;
188 /* In a more perfect world, we would hang a ptlrpc_client off of
189 * obd_type and just use the values from there. */
190 if (!strcmp(name, LUSTRE_OSC_NAME)) {
191 rq_portal = OST_REQUEST_PORTAL;
192 rp_portal = OSC_REPLY_PORTAL;
193 connect_op = OST_CONNECT;
194 } else if (!strcmp(name, LUSTRE_MDC_NAME)) {
195 rq_portal = MDS_REQUEST_PORTAL;
196 rp_portal = MDC_REPLY_PORTAL;
197 connect_op = MDS_CONNECT;
198 } else if (!strcmp(name, LUSTRE_MGMTCLI_NAME)) {
199 rq_portal = MGMT_REQUEST_PORTAL;
200 rp_portal = MGMT_REPLY_PORTAL;
201 connect_op = MGMT_CONNECT;
203 CERROR("unknown client OBD type \"%s\", can't setup\n",
209 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
210 CERROR("requires a TARGET UUID\n");
214 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
215 CERROR("client UUID must be less than 38 characters\n");
219 if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
220 CERROR("setup requires a SERVER UUID\n");
224 if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
225 CERROR("target UUID must be less than 38 characters\n");
229 sema_init(&cli->cl_sem, 1);
230 cli->cl_conn_count = 0;
231 memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
232 min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
233 sizeof(server_uuid)));
236 cli->cl_avail_grant = 0;
237 /* FIXME: should limit this for the sum of all cl_dirty_max */
238 cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
239 if (cli->cl_dirty_max >> PAGE_SHIFT > num_physpages / 8)
240 cli->cl_dirty_max = num_physpages << (PAGE_SHIFT - 3);
241 INIT_LIST_HEAD(&cli->cl_cache_waiters);
242 INIT_LIST_HEAD(&cli->cl_loi_ready_list);
243 INIT_LIST_HEAD(&cli->cl_loi_write_list);
244 INIT_LIST_HEAD(&cli->cl_loi_read_list);
245 spin_lock_init(&cli->cl_loi_list_lock);
246 cli->cl_r_in_flight = 0;
247 cli->cl_w_in_flight = 0;
248 spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
249 spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
250 spin_lock_init(&cli->cl_read_page_hist.oh_lock);
251 spin_lock_init(&cli->cl_write_page_hist.oh_lock);
253 if (num_physpages >> (20 - PAGE_SHIFT) <= 128) { /* <= 128 MB */
254 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 4;
255 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 4;
256 } else if (num_physpages >> (20 - PAGE_SHIFT) <= 512) { /* <= 512 MB */
257 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 2;
258 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 2;
260 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES;
261 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
266 CERROR("ldlm_get_ref failed: %d\n", rc);
270 ptlrpc_init_client(rq_portal, rp_portal, name,
271 &obddev->obd_ldlm_client);
273 imp = class_new_import();
275 GOTO(err_ldlm, rc = -ENOENT);
276 imp->imp_client = &obddev->obd_ldlm_client;
277 imp->imp_obd = obddev;
278 imp->imp_connect_op = connect_op;
279 imp->imp_generation = 0;
280 imp->imp_initial_recov = 1;
281 INIT_LIST_HEAD(&imp->imp_pinger_chain);
282 memcpy(imp->imp_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
283 LUSTRE_CFG_BUFLEN(lcfg, 1));
284 class_import_put(imp);
286 rc = client_import_add_conn(imp, &server_uuid, 1);
288 CERROR("can't add initial connection\n");
289 GOTO(err_import, rc);
292 cli->cl_import = imp;
293 cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
294 cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
295 cli->cl_sandev = to_kdev_t(0);
297 if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
298 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
299 CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
300 name, obddev->obd_name,
301 imp->imp_target_uuid.uuid);
302 imp->imp_invalid = 1;
304 if (LUSTRE_CFG_BUFLEN(lcfg, 4) > 0)
305 mgmt_name = lustre_cfg_string(lcfg, 4);
307 mgmt_name = lustre_cfg_string(lcfg, 3);
311 if (mgmt_name != NULL) {
312 /* Register with management client if we need to. */
313 CDEBUG(D_HA, "%s registering with %s for events about %s\n",
314 obddev->obd_name, mgmt_name, server_uuid.uuid);
316 mgmt_obd = class_name2obd(mgmt_name);
318 CERROR("can't find mgmtcli %s to register\n",
320 GOTO(err_import, rc = -ENOSYS);
323 register_f = (mgmtcli_register_for_events_t)symbol_get("mgmtcli_register_for_events");
325 CERROR("can't i_m_g mgmtcli_register_for_events\n");
326 GOTO(err_import, rc = -ENOSYS);
329 rc = register_f(mgmt_obd, obddev, &imp->imp_target_uuid);
330 symbol_put("mgmtcli_register_for_events");
333 cli->cl_mgmtcli_obd = mgmt_obd;
339 class_destroy_import(imp);
347 int client_obd_cleanup(struct obd_device *obddev, int flags)
349 struct client_obd *cli = &obddev->u.cli;
354 if (cli->cl_mgmtcli_obd) {
355 mgmtcli_deregister_for_events_t dereg_f;
357 dereg_f = (mgmtcli_deregister_for_events_t)symbol_get("mgmtcli_deregister_for_events");
358 dereg_f(cli->cl_mgmtcli_obd, obddev);
359 symbol_put("mgmtcli_deregister_for_events");
362 /* Here we try to drop the security structure after destroy import,
363 * to avoid issue of "sleep in spinlock".
365 class_import_get(cli->cl_import);
366 class_destroy_import(cli->cl_import);
367 ptlrpcs_import_drop_sec(cli->cl_import);
368 class_import_put(cli->cl_import);
369 cli->cl_import = NULL;
371 ldlm_put_ref(flags & OBD_OPT_FORCE);
376 int client_connect_import(struct lustre_handle *dlm_handle,
377 struct obd_device *obd,
378 struct obd_uuid *cluuid,
379 struct obd_connect_data *conn_data,
380 unsigned long connect_flags)
382 struct client_obd *cli = &obd->u.cli;
383 struct obd_import *imp = cli->cl_import;
384 struct obd_export *exp;
389 rc = class_connect(dlm_handle, obd, cluuid);
393 cli->cl_conn_count++;
394 if (cli->cl_conn_count > 1)
396 exp = class_conn2export(dlm_handle);
398 if (obd->obd_namespace != NULL)
399 CERROR("already have namespace!\n");
400 obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
401 LDLM_NAMESPACE_CLIENT);
402 if (obd->obd_namespace == NULL)
403 GOTO(out_disco, rc = -ENOMEM);
405 rc = ptlrpcs_import_get_sec(imp);
409 imp->imp_dlm_handle = *dlm_handle;
410 rc = ptlrpc_init_import(imp);
414 imp->imp_connect_flags = connect_flags;
416 memcpy(&imp->imp_connect_data, conn_data, sizeof(*conn_data));
418 rc = ptlrpc_connect_import(imp, NULL);
420 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
423 LASSERT(exp->exp_connection);
424 ptlrpc_pinger_add_import(imp);
429 ldlm_namespace_free(obd->obd_namespace, 0);
430 obd->obd_namespace = NULL;
432 cli->cl_conn_count--;
433 class_disconnect(exp, 0);
435 class_export_put(exp);
442 int client_disconnect_export(struct obd_export *exp, unsigned long flags)
444 struct obd_device *obd = class_exp2obd(exp);
445 struct client_obd *cli = &obd->u.cli;
446 struct obd_import *imp = cli->cl_import;
451 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
452 exp, exp ? exp->exp_handle.h_cookie : -1);
457 if (!cli->cl_conn_count) {
458 CERROR("disconnecting disconnected device (%s)\n",
460 GOTO(out_sem, rc = -EINVAL);
463 cli->cl_conn_count--;
464 if (cli->cl_conn_count)
465 GOTO(out_no_disconnect, rc = 0);
467 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
468 * delete it regardless. (It's safe to delete an import that was
470 (void)ptlrpc_pinger_del_import(imp);
472 if (obd->obd_namespace != NULL) {
473 /* obd_no_recov == local only */
474 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
475 obd->obd_no_recov, NULL);
476 ldlm_namespace_free(obd->obd_namespace, obd->obd_no_recov);
477 obd->obd_namespace = NULL;
481 * Yeah, obd_no_recov also (mainly) means "forced shutdown".
483 if (obd->obd_no_recov)
484 ptlrpc_invalidate_import(imp, 0);
486 rc = ptlrpc_disconnect_import(imp);
490 err = class_disconnect(exp, 0);
498 /* --------------------------------------------------------------------------
499 * from old lib/target.c
500 * -------------------------------------------------------------------------- */
502 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
503 struct obd_uuid *cluuid, int initial_conn)
505 if (exp->exp_connection && !initial_conn) {
506 struct lustre_handle *hdl;
507 hdl = &exp->exp_imp_reverse->imp_remote_handle;
508 /* Might be a re-connect after a partition. */
509 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
510 CERROR("%s reconnecting\n", cluuid->uuid);
511 conn->cookie = exp->exp_handle.h_cookie;
514 CERROR("%s reconnecting from %s, "
515 "handle mismatch (ours "LPX64", theirs "
516 LPX64")\n", cluuid->uuid,
517 exp->exp_connection->c_remote_uuid.uuid,
518 hdl->cookie, conn->cookie);
519 memset(conn, 0, sizeof *conn);
524 conn->cookie = exp->exp_handle.h_cookie;
525 CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
527 CDEBUG(D_IOCTL,"connect: cookie "LPX64"\n", conn->cookie);
531 static inline int ptlrpc_peer_is_local(struct ptlrpc_peer *peer)
533 ptl_process_id_t myid;
535 PtlGetId(peer->peer_ni->pni_ni_h, &myid);
536 return (memcmp(&peer->peer_id, &myid, sizeof(myid)) == 0);
539 int target_handle_connect(struct ptlrpc_request *req)
541 unsigned long connect_flags = 0, *cfp;
542 struct obd_device *target;
543 struct obd_export *export = NULL;
544 struct obd_import *revimp;
545 struct lustre_handle conn;
546 struct obd_uuid tgtuuid;
547 struct obd_uuid cluuid;
548 struct obd_uuid remote_uuid;
550 struct obd_connect_data *conn_data;
551 int conn_data_size = sizeof(*conn_data);
555 int initial_conn = 0;
556 char peer_str[PTL_NALFMT_SIZE];
557 const int offset = 1;
560 OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
562 LASSERT_REQSWAB (req, offset + 0);
563 str = lustre_msg_string(req->rq_reqmsg, offset + 0,
564 sizeof(tgtuuid) - 1);
566 CERROR("bad target UUID for connect\n");
567 GOTO(out, rc = -EINVAL);
570 obd_str2uuid (&tgtuuid, str);
571 target = class_uuid2obd(&tgtuuid);
573 target = class_name2obd(str);
575 if (!target || target->obd_stopping || !target->obd_set_up) {
576 CERROR("UUID '%s' is not available for connect from %s\n",
577 str, req->rq_peerstr);
578 GOTO(out, rc = -ENODEV);
581 LASSERT_REQSWAB (req, offset + 1);
582 str = lustre_msg_string(req->rq_reqmsg, offset + 1, sizeof(cluuid) - 1);
584 CERROR("bad client UUID for connect\n");
585 GOTO(out, rc = -EINVAL);
588 obd_str2uuid (&cluuid, str);
590 /* XXX extract a nettype and format accordingly */
591 switch (sizeof(ptl_nid_t)) {
592 /* NB the casts only avoid compiler warnings */
594 snprintf(remote_uuid.uuid, sizeof remote_uuid,
595 "NET_"LPX64"_UUID", (__u64)req->rq_peer.peer_id.nid);
598 snprintf(remote_uuid.uuid, sizeof remote_uuid,
599 "NET_%x_UUID", (__u32)req->rq_peer.peer_id.nid);
605 tmp = lustre_msg_buf(req->rq_reqmsg, offset + 2, sizeof conn);
607 GOTO(out, rc = -EPROTO);
609 memcpy(&conn, tmp, sizeof conn);
611 cfp = lustre_msg_buf(req->rq_reqmsg, offset + 3, sizeof(unsigned long));
612 LASSERT(cfp != NULL);
613 connect_flags = *cfp;
615 conn_data = lustre_swab_reqbuf(req, offset + 4, sizeof(*conn_data),
616 lustre_swab_connect);
618 GOTO(out, rc = -EPROTO);
620 rc = lustre_pack_reply(req, 1, &conn_data_size, NULL);
624 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_INITIAL)
627 /* lctl gets a backstage, all-access pass. */
628 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
629 goto dont_check_exports;
631 spin_lock(&target->obd_dev_lock);
632 list_for_each(p, &target->obd_exports) {
633 export = list_entry(p, struct obd_export, exp_obd_chain);
634 if (obd_uuid_equals(&cluuid, &export->exp_client_uuid)) {
635 spin_unlock(&target->obd_dev_lock);
636 LASSERT(export->exp_obd == target);
638 rc = target_handle_reconnect(&conn, export, &cluuid,
644 /* If we found an export, we already unlocked. */
646 spin_unlock(&target->obd_dev_lock);
647 } else if (req->rq_export == NULL &&
648 atomic_read(&export->exp_rpc_count) > 0) {
649 CWARN("%s: refuse connection from %s/%s to 0x%p/%d\n",
650 target->obd_name, cluuid.uuid,
651 ptlrpc_peernid2str(&req->rq_peer, peer_str),
652 export, atomic_read(&export->exp_refcount));
653 GOTO(out, rc = -EBUSY);
654 } else if (req->rq_export != NULL &&
655 atomic_read(&export->exp_rpc_count) > 1) {
656 CWARN("%s: refuse reconnection from %s@%s to 0x%p/%d\n",
657 target->obd_name, cluuid.uuid,
658 ptlrpc_peernid2str(&req->rq_peer, peer_str),
659 export, atomic_read(&export->exp_rpc_count));
660 GOTO(out, rc = -EBUSY);
661 } else if (req->rq_reqmsg->conn_cnt == 1 && !initial_conn) {
662 CERROR("%s reconnected with 1 conn_cnt; cookies not random?\n",
664 GOTO(out, rc = -EALREADY);
667 /* Tell the client if we're in recovery. */
668 /* If this is the first client, start the recovery timer */
669 CWARN("%s: connection from %s@%s/%lu %st"LPU64"\n", target->obd_name, cluuid.uuid,
670 ptlrpc_peernid2str(&req->rq_peer, peer_str), *cfp,
671 target->obd_recovering ? "recovering/" : "",
674 if (target->obd_recovering) {
675 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
676 target_start_recovery_timer(target);
680 /* Tell the client if we support replayable requests */
681 if (target->obd_replayable)
682 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
685 if (export == NULL) {
686 if (target->obd_recovering) {
687 CERROR("%s denying connection for new client %s@%s: "
688 "%d clients in recovery for %lds\n", target->obd_name,
690 ptlrpc_peernid2str(&req->rq_peer, peer_str),
691 target->obd_recoverable_clients,
692 (target->obd_recovery_timer.expires-jiffies)/HZ);
696 rc = obd_connect(&conn, target, &cluuid, conn_data,
701 /* Return only the parts of obd_connect_data that we understand, so the
702 * client knows that we don't understand the rest. */
703 conn_data->ocd_connect_flags &= OBD_CONNECT_SUPPORTED;
704 memcpy(lustre_msg_buf(req->rq_repmsg, 0, sizeof(*conn_data)), conn_data,
707 /* Tell the client if we support replayable requests */
708 if (target->obd_replayable)
709 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
711 /* If all else goes well, this is our RPC return code. */
714 if (rc && rc != EALREADY)
717 req->rq_repmsg->handle = conn;
719 /* If the client and the server are the same node, we will already
720 * have an export that really points to the client's DLM export,
721 * because we have a shared handles table.
723 * XXX this will go away when shaver stops sending the "connect" handle
724 * in the real "remote handle" field of the request --phik 24 Apr 2003
726 if (req->rq_export != NULL)
727 class_export_put(req->rq_export);
729 /* ownership of this export ref transfers to the request */
730 export = req->rq_export = class_conn2export(&conn);
731 LASSERT(export != NULL);
733 spin_lock_irqsave(&export->exp_lock, flags);
735 req->rq_repmsg->conn_cnt = export->exp_conn_cnt + 1;
736 } else if (export->exp_conn_cnt >= req->rq_reqmsg->conn_cnt) {
737 CERROR("%s@%s: already connected at a higher conn_cnt: %d > %d\n",
738 cluuid.uuid, ptlrpc_peernid2str(&req->rq_peer, peer_str),
739 export->exp_conn_cnt,
740 req->rq_reqmsg->conn_cnt);
741 spin_unlock_irqrestore(&export->exp_lock, flags);
742 GOTO(out, rc = -EALREADY);
744 export->exp_conn_cnt = req->rq_reqmsg->conn_cnt;
745 spin_unlock_irqrestore(&export->exp_lock, flags);
747 /* request from liblustre? */
748 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT)
749 export->exp_libclient = 1;
751 if (!(lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_ASYNC) &&
752 ptlrpc_peer_is_local(&req->rq_peer)) {
753 CWARN("%s: exp %p set sync\n", target->obd_name, export);
754 export->exp_sync = 1;
756 CDEBUG(D_HA, "%s: exp %p set async\n",target->obd_name,export);
757 export->exp_sync = 0;
760 if (export->exp_connection != NULL)
761 ptlrpc_put_connection(export->exp_connection);
762 export->exp_connection = ptlrpc_get_connection(&req->rq_peer,
765 if (rc == EALREADY) {
766 /* We indicate the reconnection in a flag, not an error code. */
767 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
771 spin_lock_bh(&target->obd_processing_task_lock);
772 if (target->obd_recovering && export->exp_connected == 0) {
773 __u64 t = conn_data->transno;
774 export->exp_connected = 1;
775 if ((lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_TRANSNO)
776 && t < target->obd_next_recovery_transno)
777 target->obd_next_recovery_transno = t;
778 target->obd_connected_clients++;
779 if (target->obd_connected_clients == target->obd_max_recoverable_clients)
780 wake_up(&target->obd_next_transno_waitq);
782 spin_unlock_bh(&target->obd_processing_task_lock);
784 memcpy(&conn, lustre_msg_buf(req->rq_reqmsg, offset + 2, sizeof(conn)),
787 if (export->exp_imp_reverse != NULL) {
788 /* same logic as client_obd_cleanup */
789 class_import_get(export->exp_imp_reverse);
790 class_destroy_import(export->exp_imp_reverse);
791 ptlrpcs_import_drop_sec(export->exp_imp_reverse);
792 class_import_put(export->exp_imp_reverse);
795 /* for the rest part, we return -ENOTCONN in case of errors
796 * in order to let client initialize connection again.
798 revimp = export->exp_imp_reverse = class_new_import();
800 CERROR("fail to alloc new reverse import.\n");
801 GOTO(out, rc = -ENOTCONN);
804 revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
805 revimp->imp_client = &export->exp_obd->obd_ldlm_client;
806 revimp->imp_remote_handle = conn;
807 revimp->imp_obd = target;
808 revimp->imp_dlm_fake = 1;
809 revimp->imp_state = LUSTRE_IMP_FULL;
811 rc = ptlrpcs_import_get_sec(revimp);
813 CERROR("reverse import can not get sec: %d\n", rc);
814 class_destroy_import(revimp);
815 export->exp_imp_reverse = NULL;
816 GOTO(out, rc = -ENOTCONN);
819 class_import_put(revimp);
821 rc = obd_connect_post(export, initial_conn, connect_flags);
828 int target_handle_disconnect(struct ptlrpc_request *req)
830 struct obd_export *exp;
834 rc = lustre_pack_reply(req, 0, NULL, NULL);
838 /* keep the rq_export around so we can send the reply */
839 exp = class_export_get(req->rq_export);
840 req->rq_status = obd_disconnect(exp, 0);
844 void target_destroy_export(struct obd_export *exp)
846 /* exports created from last_rcvd data, and "fake"
847 exports created by lctl don't have an import */
848 if (exp->exp_imp_reverse != NULL) {
849 ptlrpcs_import_drop_sec(exp->exp_imp_reverse);
850 class_destroy_import(exp->exp_imp_reverse);
853 /* We cancel locks at disconnect time, but this will catch any locks
854 * granted in a race with recovery-induced disconnect. */
855 if (exp->exp_obd->obd_namespace != NULL)
856 ldlm_cancel_locks_for_export(exp);
863 struct ptlrpc_request *
864 ptlrpc_clone_req( struct ptlrpc_request *orig_req)
866 struct ptlrpc_request *copy_req;
867 struct lustre_msg *copy_reqmsg;
869 OBD_ALLOC(copy_req, sizeof *copy_req);
872 OBD_ALLOC(copy_reqmsg, orig_req->rq_reqlen);
874 OBD_FREE(copy_req, sizeof *copy_req);
878 memcpy(copy_req, orig_req, sizeof *copy_req);
879 memcpy(copy_reqmsg, orig_req->rq_reqmsg, orig_req->rq_reqlen);
880 /* the copied req takes over the reply state and security data */
881 orig_req->rq_reply_state = NULL;
882 orig_req->rq_sec_svcdata = NULL;
884 copy_req->rq_reqmsg = copy_reqmsg;
885 class_export_get(copy_req->rq_export);
886 INIT_LIST_HEAD(©_req->rq_list);
891 void ptlrpc_free_clone( struct ptlrpc_request *req)
894 svcsec_cleanup_req(req);
896 class_export_put(req->rq_export);
897 list_del(&req->rq_list);
898 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
899 OBD_FREE(req, sizeof *req);
902 static void target_release_saved_req(struct ptlrpc_request *req)
905 svcsec_cleanup_req(req);
907 class_export_put(req->rq_export);
908 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
909 OBD_FREE(req, sizeof *req);
912 static void target_finish_recovery(struct obd_device *obd)
916 ldlm_reprocess_all_ns(obd->obd_namespace);
918 /* when recovery finished, cleanup orphans on mds and ost */
919 if (OBT(obd) && OBP(obd, postrecov)) {
920 rc = OBP(obd, postrecov)(obd);
922 CWARN("%s: all clients recovered, %d MDS "
923 "orphans deleted\n", obd->obd_name, rc);
925 CERROR("postrecov failed %d\n", rc);
928 obd->obd_recovery_end = LTIME_S(CURRENT_TIME);
932 static void abort_req_replay_queue(struct obd_device *obd)
934 struct ptlrpc_request *req;
935 struct list_head *tmp, *n;
938 list_for_each_safe(tmp, n, &obd->obd_req_replay_queue) {
939 req = list_entry(tmp, struct ptlrpc_request, rq_list);
940 list_del(&req->rq_list);
941 DEBUG_REQ(D_ERROR, req, "aborted:");
942 req->rq_status = -ENOTCONN;
943 req->rq_type = PTL_RPC_MSG_ERR;
944 rc = lustre_pack_reply(req, 0, NULL, NULL);
948 DEBUG_REQ(D_ERROR, req,
949 "packing failed for abort-reply; skipping");
951 target_release_saved_req(req);
955 static void abort_lock_replay_queue(struct obd_device *obd)
957 struct ptlrpc_request *req;
958 struct list_head *tmp, *n;
961 list_for_each_safe(tmp, n, &obd->obd_lock_replay_queue) {
962 req = list_entry(tmp, struct ptlrpc_request, rq_list);
963 list_del(&req->rq_list);
964 DEBUG_REQ(D_ERROR, req, "aborted:");
965 req->rq_status = -ENOTCONN;
966 req->rq_type = PTL_RPC_MSG_ERR;
967 rc = lustre_pack_reply(req, 0, NULL, NULL);
971 DEBUG_REQ(D_ERROR, req,
972 "packing failed for abort-reply; skipping");
974 target_release_saved_req(req);
978 /* Called from a cleanup function if the device is being cleaned up
979 forcefully. The exports should all have been disconnected already,
980 the only thing left to do is
981 - clear the recovery flags
983 - free queued requests and replies, but don't send replies
984 Because the obd_stopping flag is set, no new requests should be received.
987 void target_cleanup_recovery(struct obd_device *obd)
989 struct list_head *tmp, *n;
990 struct ptlrpc_request *req;
992 spin_lock_bh(&obd->obd_processing_task_lock);
993 if (!obd->obd_recovering) {
994 spin_unlock_bh(&obd->obd_processing_task_lock);
998 obd->obd_recovering = obd->obd_abort_recovery = 0;
999 target_cancel_recovery_timer(obd);
1000 spin_unlock_bh(&obd->obd_processing_task_lock);
1002 list_for_each_safe(tmp, n, &obd->obd_req_replay_queue) {
1003 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1004 list_del(&req->rq_list);
1005 LASSERT (req->rq_reply_state == 0);
1006 target_release_saved_req(req);
1008 list_for_each_safe(tmp, n, &obd->obd_lock_replay_queue) {
1009 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1010 list_del(&req->rq_list);
1011 LASSERT (req->rq_reply_state == 0);
1012 target_release_saved_req(req);
1014 list_for_each_safe(tmp, n, &obd->obd_final_req_queue) {
1015 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1016 list_del(&req->rq_list);
1017 LASSERT (req->rq_reply_state == 0);
1018 target_release_saved_req(req);
1023 static void target_abort_recovery(void *data)
1025 struct obd_device *obd = data;
1027 LASSERT(!obd->obd_recovering);
1029 class_disconnect_stale_exports(obd, 0);
1031 CERROR("%s: recovery period over; disconnecting unfinished clients.\n",
1034 abort_recovery_queue(obd);
1035 target_finish_recovery(obd);
1036 ptlrpc_run_recovery_over_upcall(obd);
1040 static void target_recovery_expired(unsigned long castmeharder)
1042 struct obd_device *obd = (struct obd_device *)castmeharder;
1043 spin_lock_bh(&obd->obd_processing_task_lock);
1044 if (obd->obd_recovering)
1045 obd->obd_abort_recovery = 1;
1047 wake_up(&obd->obd_next_transno_waitq);
1048 spin_unlock_bh(&obd->obd_processing_task_lock);
1052 /* obd_processing_task_lock should be held */
1053 void target_cancel_recovery_timer(struct obd_device *obd)
1055 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
1056 del_timer(&obd->obd_recovery_timer);
1060 static void reset_recovery_timer(struct obd_device *obd)
1062 spin_lock_bh(&obd->obd_processing_task_lock);
1063 if (!obd->obd_recovering) {
1064 spin_unlock_bh(&obd->obd_processing_task_lock);
1067 CDEBUG(D_HA, "timer will expire in %u seconds\n",
1068 OBD_RECOVERY_TIMEOUT / HZ);
1069 mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
1070 spin_unlock_bh(&obd->obd_processing_task_lock);
1074 /* Only start it the first time called */
1075 void target_start_recovery_timer(struct obd_device *obd)
1077 spin_lock_bh(&obd->obd_processing_task_lock);
1078 if (!obd->obd_recovering || timer_pending(&obd->obd_recovery_timer)) {
1079 spin_unlock_bh(&obd->obd_processing_task_lock);
1082 CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
1083 OBD_RECOVERY_TIMEOUT / HZ);
1084 obd->obd_recovery_timer.function = target_recovery_expired;
1085 obd->obd_recovery_timer.data = (unsigned long)obd;
1086 mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
1087 spin_unlock_bh(&obd->obd_processing_task_lock);
1091 static int check_for_next_transno(struct obd_device *obd)
1093 struct ptlrpc_request *req = NULL;
1094 int wake_up = 0, connected, completed, queue_len, max;
1095 __u64 next_transno, req_transno;
1097 spin_lock_bh(&obd->obd_processing_task_lock);
1098 if (!list_empty(&obd->obd_req_replay_queue)) {
1099 req = list_entry(obd->obd_req_replay_queue.next,
1100 struct ptlrpc_request, rq_list);
1101 req_transno = req->rq_reqmsg->transno;
1106 max = obd->obd_max_recoverable_clients;
1107 connected = obd->obd_connected_clients;
1108 completed = max - obd->obd_recoverable_clients;
1109 queue_len = obd->obd_requests_queued_for_recovery;
1110 next_transno = obd->obd_next_recovery_transno;
1112 CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
1113 "req_transno: "LPU64", next_transno: "LPU64"\n",
1114 max, connected, completed, queue_len, req_transno, next_transno);
1115 if (obd->obd_abort_recovery) {
1116 CDEBUG(D_HA, "waking for aborted recovery\n");
1118 } else if (atomic_read(&obd->obd_req_replay_clients) == 0) {
1119 CDEBUG(D_HA, "waking for completed recovery\n");
1121 } else if (req_transno == next_transno) {
1122 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1124 } else if (queue_len + completed == max) {
1125 LASSERT(req->rq_reqmsg->transno >= next_transno);
1126 if (req_transno > obd->obd_last_committed)
1127 CDEBUG(D_ERROR, "waking for skipped transno (skip: "
1128 LPD64", ql: %d, comp: %d, conn: %d, next: "
1129 LPD64")\n", next_transno, queue_len, completed,
1131 obd->obd_next_recovery_transno = req_transno;
1133 } else if (queue_len == atomic_read(&obd->obd_req_replay_clients)) {
1134 /* some clients haven't connected in time, but we need
1135 * their requests to continue recovery. so, we abort ... */
1136 CDEBUG(D_ERROR, "abort due to missed clients: queue: %d max: %d\n",
1138 obd->obd_abort_recovery = 1;
1141 spin_unlock_bh(&obd->obd_processing_task_lock);
1146 static struct ptlrpc_request *
1147 target_next_replay_req(struct obd_device *obd)
1149 struct l_wait_info lwi = { 0 };
1150 struct ptlrpc_request *req;
1152 CDEBUG(D_HA, "Waiting for transno "LPD64"\n",
1153 obd->obd_next_recovery_transno);
1154 l_wait_event(obd->obd_next_transno_waitq,
1155 check_for_next_transno(obd), &lwi);
1157 spin_lock_bh(&obd->obd_processing_task_lock);
1158 if (obd->obd_abort_recovery) {
1160 } else if (!list_empty(&obd->obd_req_replay_queue)) {
1161 req = list_entry(obd->obd_req_replay_queue.next,
1162 struct ptlrpc_request, rq_list);
1163 list_del_init(&req->rq_list);
1164 obd->obd_requests_queued_for_recovery--;
1168 spin_unlock_bh(&obd->obd_processing_task_lock);
1172 static int check_for_next_lock(struct obd_device *obd)
1174 struct ptlrpc_request *req = NULL;
1177 spin_lock_bh(&obd->obd_processing_task_lock);
1178 if (!list_empty(&obd->obd_lock_replay_queue)) {
1179 req = list_entry(obd->obd_lock_replay_queue.next,
1180 struct ptlrpc_request, rq_list);
1181 CDEBUG(D_HA, "waking for next lock\n");
1183 } else if (atomic_read(&obd->obd_lock_replay_clients) == 0) {
1184 CDEBUG(D_HA, "waking for completed lock replay\n");
1186 } else if (obd->obd_abort_recovery) {
1187 CDEBUG(D_HA, "waking for aborted recovery\n");
1190 spin_unlock_bh(&obd->obd_processing_task_lock);
1195 static struct ptlrpc_request *
1196 target_next_replay_lock(struct obd_device *obd)
1198 struct l_wait_info lwi = { 0 };
1199 struct ptlrpc_request *req;
1201 CDEBUG(D_HA, "Waiting for lock\n");
1202 l_wait_event(obd->obd_next_transno_waitq,
1203 check_for_next_lock(obd), &lwi);
1205 spin_lock_bh(&obd->obd_processing_task_lock);
1206 if (obd->obd_abort_recovery) {
1208 } else if (!list_empty(&obd->obd_lock_replay_queue)) {
1209 req = list_entry(obd->obd_lock_replay_queue.next,
1210 struct ptlrpc_request, rq_list);
1211 list_del_init(&req->rq_list);
1215 spin_unlock_bh(&obd->obd_processing_task_lock);
1219 static struct ptlrpc_request *
1220 target_next_final_ping(struct obd_device *obd)
1222 struct ptlrpc_request *req;
1224 spin_lock_bh(&obd->obd_processing_task_lock);
1225 if (!list_empty(&obd->obd_final_req_queue)) {
1226 req = list_entry(obd->obd_final_req_queue.next,
1227 struct ptlrpc_request, rq_list);
1228 list_del_init(&req->rq_list);
1232 spin_unlock_bh(&obd->obd_processing_task_lock);
1236 static int req_replay_done(struct obd_export *exp)
1238 if (exp->exp_req_replay_needed)
1243 static int lock_replay_done(struct obd_export *exp)
1245 if (exp->exp_lock_replay_needed)
1250 static int connect_done(struct obd_export *exp)
1252 if (exp->exp_connected)
1257 static int check_for_clients(struct obd_device *obd)
1259 if (obd->obd_abort_recovery)
1261 LASSERT(obd->obd_connected_clients <= obd->obd_max_recoverable_clients);
1262 if (obd->obd_connected_clients == obd->obd_max_recoverable_clients)
1267 static int target_recovery_thread(void *arg)
1269 struct obd_device *obd = arg;
1270 struct ptlrpc_request *req;
1271 struct target_recovery_data *trd = &obd->obd_recovery_data;
1272 char peer_str[PTL_NALFMT_SIZE];
1273 struct l_wait_info lwi = { 0 };
1274 unsigned long flags;
1277 kportal_daemonize("tgt-recov");
1279 SIGNAL_MASK_LOCK(current, flags);
1280 sigfillset(¤t->blocked);
1282 SIGNAL_MASK_UNLOCK(current, flags);
1284 CERROR("%s: started recovery thread pid %d\n", obd->obd_name,
1286 trd->trd_processing_task = current->pid;
1288 obd->obd_recovering = 1;
1289 complete(&trd->trd_starting);
1291 /* first of all, we have to know the first transno to replay */
1292 obd->obd_abort_recovery = 0;
1293 l_wait_event(obd->obd_next_transno_waitq,
1294 check_for_clients(obd), &lwi);
1296 spin_lock_bh(&obd->obd_processing_task_lock);
1297 target_cancel_recovery_timer(obd);
1298 spin_unlock_bh(&obd->obd_processing_task_lock);
1300 /* If some clients haven't connected in time, evict them */
1301 if (obd->obd_abort_recovery) {
1303 CERROR("some clients haven't connect in time, evict them ...\n");
1304 obd->obd_abort_recovery = 0;
1305 stale = class_disconnect_stale_exports(obd, connect_done, 0);
1306 atomic_sub(stale, &obd->obd_req_replay_clients);
1307 atomic_sub(stale, &obd->obd_lock_replay_clients);
1310 /* next stage: replay requests */
1311 CWARN("1: request replay stage - %d clients from t"LPU64"\n",
1312 atomic_read(&obd->obd_req_replay_clients),
1313 obd->obd_next_recovery_transno);
1314 while ((req = target_next_replay_req(obd))) {
1315 LASSERT(trd->trd_processing_task == current->pid);
1316 DEBUG_REQ(D_HA, req, "processing t"LPD64" from %s: ",
1317 req->rq_reqmsg->transno,
1318 ptlrpc_peernid2str(&req->rq_peer, peer_str));
1319 (void)trd->trd_recovery_handler(req);
1320 obd->obd_replayed_requests++;
1321 reset_recovery_timer(obd);
1322 /* bug 1580: decide how to properly sync() in recovery*/
1323 //mds_fsync_super(mds->mds_sb);
1324 ptlrpc_free_clone(req);
1325 spin_lock_bh(&obd->obd_processing_task_lock);
1326 obd->obd_next_recovery_transno++;
1327 spin_unlock_bh(&obd->obd_processing_task_lock);
1330 spin_lock_bh(&obd->obd_processing_task_lock);
1331 target_cancel_recovery_timer(obd);
1332 spin_unlock_bh(&obd->obd_processing_task_lock);
1334 /* If some clients haven't replayed requests in time, evict them */
1335 if (obd->obd_abort_recovery) {
1337 CERROR("req replay timed out, aborting ...\n");
1338 obd->obd_abort_recovery = 0;
1339 stale = class_disconnect_stale_exports(obd, req_replay_done, 0);
1340 atomic_sub(stale, &obd->obd_lock_replay_clients);
1341 abort_req_replay_queue(obd);
1344 /* The second stage: replay locks */
1345 CWARN("2: lock replay stage - %d clients\n",
1346 atomic_read(&obd->obd_lock_replay_clients));
1347 while ((req = target_next_replay_lock(obd))) {
1348 LASSERT(trd->trd_processing_task == current->pid);
1349 DEBUG_REQ(D_HA, req, "processing lock from %s: ",
1350 ptlrpc_peernid2str(&req->rq_peer, peer_str));
1351 (void)trd->trd_recovery_handler(req);
1352 reset_recovery_timer(obd);
1353 ptlrpc_free_clone(req);
1354 obd->obd_replayed_locks++;
1357 spin_lock_bh(&obd->obd_processing_task_lock);
1358 target_cancel_recovery_timer(obd);
1359 spin_unlock_bh(&obd->obd_processing_task_lock);
1361 /* If some clients haven't replayed requests in time, evict them */
1362 if (obd->obd_abort_recovery) {
1364 CERROR("lock replay timed out, aborting ...\n");
1365 obd->obd_abort_recovery = 0;
1366 stale = class_disconnect_stale_exports(obd, lock_replay_done, 0);
1367 abort_lock_replay_queue(obd);
1370 /* We drop recoverying flag to forward all new requests
1371 * to regular mds_handle() since now */
1372 spin_lock_bh(&obd->obd_processing_task_lock);
1373 obd->obd_recovering = 0;
1374 spin_unlock_bh(&obd->obd_processing_task_lock);
1376 /* The third stage: reply on final pings */
1377 CWARN("3: final stage - process recovery completion pings\n");
1378 while ((req = target_next_final_ping(obd))) {
1379 LASSERT(trd->trd_processing_task == current->pid);
1380 DEBUG_REQ(D_HA, req, "processing final ping from %s: ",
1381 ptlrpc_peernid2str(&req->rq_peer, peer_str));
1382 (void)trd->trd_recovery_handler(req);
1383 ptlrpc_free_clone(req);
1386 CWARN("4: recovery completed - %d/%d reqs/locks replayed\n",
1387 obd->obd_replayed_requests, obd->obd_replayed_locks);
1388 target_finish_recovery(obd);
1390 trd->trd_processing_task = 0;
1391 complete(&trd->trd_finishing);
1395 int target_start_recovery_thread(struct obd_device *obd, svc_handler_t handler)
1398 struct target_recovery_data *trd = &obd->obd_recovery_data;
1400 memset(trd, 0, sizeof(*trd));
1401 init_completion(&trd->trd_starting);
1402 init_completion(&trd->trd_finishing);
1403 trd->trd_recovery_handler = handler;
1405 if (kernel_thread(target_recovery_thread, obd, 0) > 0) {
1406 wait_for_completion(&trd->trd_starting);
1407 LASSERT(obd->obd_recovering != 0);
1414 void target_stop_recovery_thread(struct obd_device *obd)
1416 spin_lock_bh(&obd->obd_processing_task_lock);
1417 if (obd->obd_recovery_data.trd_processing_task > 0) {
1418 struct target_recovery_data *trd = &obd->obd_recovery_data;
1419 CERROR("%s: aborting recovery\n", obd->obd_name);
1420 obd->obd_abort_recovery = 1;
1421 wake_up(&obd->obd_next_transno_waitq);
1422 spin_unlock_bh(&obd->obd_processing_task_lock);
1423 wait_for_completion(&trd->trd_finishing);
1425 spin_unlock_bh(&obd->obd_processing_task_lock);
1430 int target_process_req_flags(struct obd_device *obd, struct ptlrpc_request *req)
1432 struct obd_export *exp = req->rq_export;
1433 LASSERT(exp != NULL);
1434 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
1435 /* client declares he's ready to replay locks */
1436 spin_lock_bh(&obd->obd_processing_task_lock);
1437 if (exp->exp_req_replay_needed) {
1438 LASSERT(atomic_read(&obd->obd_req_replay_clients) > 0);
1439 exp->exp_req_replay_needed = 0;
1440 atomic_dec(&obd->obd_req_replay_clients);
1441 obd->obd_recoverable_clients--;
1442 if (atomic_read(&obd->obd_req_replay_clients) == 0) {
1443 CDEBUG(D_HA, "all clients have replayed reqs\n");
1444 wake_up(&obd->obd_next_transno_waitq);
1447 spin_unlock_bh(&obd->obd_processing_task_lock);
1449 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
1450 /* client declares he's ready to complete recovery
1451 * so, we put the request on th final queue */
1452 spin_lock_bh(&obd->obd_processing_task_lock);
1453 if (exp->exp_lock_replay_needed) {
1454 LASSERT(atomic_read(&obd->obd_lock_replay_clients) > 0);
1455 exp->exp_lock_replay_needed = 0;
1456 atomic_dec(&obd->obd_lock_replay_clients);
1457 if (atomic_read(&obd->obd_lock_replay_clients) == 0) {
1458 CDEBUG(D_HA, "all clients have replayed locks\n");
1459 wake_up(&obd->obd_next_transno_waitq);
1462 spin_unlock_bh(&obd->obd_processing_task_lock);
1468 int target_queue_recovery_request(struct ptlrpc_request *req,
1469 struct obd_device *obd)
1471 struct list_head *tmp;
1473 __u64 transno = req->rq_reqmsg->transno;
1475 if (obd->obd_recovery_data.trd_processing_task == current->pid) {
1476 /* Processing the queue right now, don't re-add. */
1480 target_process_req_flags(obd, req);
1482 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
1483 /* client declares he's ready to complete recovery
1484 * so, we put the request on th final queue */
1485 req = ptlrpc_clone_req(req);
1488 DEBUG_REQ(D_HA, req, "queue final req");
1489 spin_lock_bh(&obd->obd_processing_task_lock);
1490 list_add_tail(&req->rq_list, &obd->obd_final_req_queue);
1491 spin_unlock_bh(&obd->obd_processing_task_lock);
1494 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
1495 /* client declares he's ready to replay locks */
1496 req = ptlrpc_clone_req(req);
1499 DEBUG_REQ(D_HA, req, "queue lock replay req");
1500 spin_lock_bh(&obd->obd_processing_task_lock);
1501 list_add_tail(&req->rq_list, &obd->obd_lock_replay_queue);
1502 spin_unlock_bh(&obd->obd_processing_task_lock);
1503 wake_up(&obd->obd_next_transno_waitq);
1508 /* CAVEAT EMPTOR: The incoming request message has been swabbed
1509 * (i.e. buflens etc are in my own byte order), but type-dependent
1510 * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1513 INIT_LIST_HEAD(&req->rq_list);
1514 DEBUG_REQ(D_HA, req, "not queueing");
1519 /* If we're processing the queue, we want don't want to queue this
1522 * Also, if this request has a transno less than the one we're waiting
1523 * for, we should process it now. It could (and currently always will)
1524 * be an open request for a descriptor that was opened some time ago.
1526 * Also, a resent, replayed request that has already been
1527 * handled will pass through here and be processed immediately.
1529 spin_lock_bh(&obd->obd_processing_task_lock);
1530 if (transno < obd->obd_next_recovery_transno && check_for_clients(obd)) {
1531 /* Processing the queue right now, don't re-add. */
1532 LASSERT(list_empty(&req->rq_list));
1533 spin_unlock_bh(&obd->obd_processing_task_lock);
1536 spin_unlock_bh(&obd->obd_processing_task_lock);
1538 /* A resent, replayed request that is still on the queue; just drop it.
1539 The queued request will handle this. */
1540 if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT | MSG_REPLAY))
1541 == (MSG_RESENT | MSG_REPLAY)) {
1542 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1546 req = ptlrpc_clone_req(req);
1550 spin_lock_bh(&obd->obd_processing_task_lock);
1553 list_for_each(tmp, &obd->obd_req_replay_queue) {
1554 struct ptlrpc_request *reqiter =
1555 list_entry(tmp, struct ptlrpc_request, rq_list);
1557 if (reqiter->rq_reqmsg->transno > transno) {
1558 list_add_tail(&req->rq_list, &reqiter->rq_list);
1565 list_add_tail(&req->rq_list, &obd->obd_req_replay_queue);
1567 obd->obd_requests_queued_for_recovery++;
1568 wake_up(&obd->obd_next_transno_waitq);
1569 spin_unlock_bh(&obd->obd_processing_task_lock);
1573 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1575 return req->rq_export->exp_obd;
1579 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1581 if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1582 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1583 DEBUG_REQ(D_ERROR, req, "dropping reply");
1584 /* NB this does _not_ send with ACK disabled, to simulate
1585 * sending OK, but timing out for the ACK */
1586 if (req->rq_reply_state != NULL) {
1587 if (!req->rq_reply_state->rs_difficult) {
1588 lustre_free_reply_state (req->rq_reply_state);
1589 req->rq_reply_state = NULL;
1591 struct ptlrpc_service *svc =
1592 req->rq_rqbd->rqbd_srv_ni->sni_service;
1593 atomic_inc(&svc->srv_outstanding_replies);
1600 req->rq_status = rc;
1601 return (ptlrpc_error(req));
1603 DEBUG_REQ(D_NET, req, "sending reply");
1606 return (ptlrpc_send_reply(req, 1));
1610 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1613 unsigned long flags;
1614 struct ptlrpc_reply_state *rs;
1615 struct obd_device *obd;
1616 struct obd_export *exp;
1617 struct ptlrpc_srv_ni *sni;
1618 struct ptlrpc_service *svc;
1620 sni = req->rq_rqbd->rqbd_srv_ni;
1621 svc = sni->sni_service;
1623 rs = req->rq_reply_state;
1624 if (rs == NULL || !rs->rs_difficult) {
1625 /* The easy case; no notifiers and reply_out_callback()
1626 * cleans up (i.e. we can't look inside rs after a
1627 * successful send) */
1628 netrc = target_send_reply_msg (req, rc, fail_id);
1630 LASSERT (netrc == 0 || req->rq_reply_state == NULL);
1634 /* must be an export if locks saved */
1635 LASSERT (req->rq_export != NULL);
1636 /* req/reply consistent */
1637 LASSERT (rs->rs_srv_ni == sni);
1640 LASSERT (!rs->rs_scheduled);
1641 LASSERT (!rs->rs_scheduled_ever);
1642 LASSERT (!rs->rs_handled);
1643 LASSERT (!rs->rs_on_net);
1644 LASSERT (rs->rs_export == NULL);
1645 LASSERT (list_empty(&rs->rs_obd_list));
1646 LASSERT (list_empty(&rs->rs_exp_list));
1648 exp = class_export_get (req->rq_export);
1651 /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1652 rs->rs_scheduled = 1;
1654 rs->rs_xid = req->rq_xid;
1655 rs->rs_transno = req->rq_transno;
1656 rs->rs_export = exp;
1658 spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
1660 if (rs->rs_transno > obd->obd_last_committed) {
1661 /* not committed already */
1662 list_add_tail (&rs->rs_obd_list,
1663 &obd->obd_uncommitted_replies);
1666 spin_unlock (&obd->obd_uncommitted_replies_lock);
1667 spin_lock (&exp->exp_lock);
1669 list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1671 spin_unlock_irqrestore (&exp->exp_lock, flags);
1673 netrc = target_send_reply_msg (req, rc, fail_id);
1675 spin_lock_irqsave (&svc->srv_lock, flags);
1677 svc->srv_n_difficult_replies++;
1679 if (netrc != 0) /* error sending: reply is off the net */
1682 if (!rs->rs_on_net || /* some notifier */
1683 list_empty(&rs->rs_exp_list) || /* completed already */
1684 list_empty(&rs->rs_obd_list)) {
1685 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1686 wake_up (&svc->srv_waitq);
1688 list_add (&rs->rs_list, &sni->sni_active_replies);
1689 rs->rs_scheduled = 0; /* allow notifier to schedule */
1692 spin_unlock_irqrestore (&svc->srv_lock, flags);
1695 int target_handle_ping(struct ptlrpc_request *req)
1697 return lustre_pack_reply(req, 0, NULL, NULL);