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 memset(&cli->cl_last_write_time, 0,
260 sizeof(cli->cl_last_write_time));
262 cli->cl_write_gap_sum = 0;
263 cli->cl_write_gaps = 0;
264 cli->cl_write_num = 0;
265 cli->cl_read_num = 0;
266 cli->cl_cache_wait_num = 0;
267 cli->cl_cache_wait_sum = 0;
269 cli->cl_dirty_num = 0;
270 cli->cl_dirty_sum = 0;
271 cli->cl_dirty_av = 0;
272 cli->cl_dirty_dmax = 0;
273 cli->cl_dirty_dmin = 0;
274 cli->cl_sync_rpcs = 0;
276 if (num_physpages >> (20 - PAGE_SHIFT) <= 128) { /* <= 128 MB */
277 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 4;
278 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 4;
280 } else if (num_physpages >> (20 - PAGE_SHIFT) <= 512) { /* <= 512 MB */
281 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 2;
282 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 2;
285 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES;
286 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
291 CERROR("ldlm_get_ref failed: %d\n", rc);
295 ptlrpc_init_client(rq_portal, rp_portal, name,
296 &obddev->obd_ldlm_client);
298 imp = class_new_import();
300 GOTO(err_ldlm, rc = -ENOENT);
301 imp->imp_client = &obddev->obd_ldlm_client;
302 imp->imp_obd = obddev;
303 imp->imp_connect_op = connect_op;
304 imp->imp_generation = 0;
305 imp->imp_initial_recov = 1;
306 INIT_LIST_HEAD(&imp->imp_pinger_chain);
307 memcpy(imp->imp_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
308 LUSTRE_CFG_BUFLEN(lcfg, 1));
309 class_import_put(imp);
311 rc = client_import_add_conn(imp, &server_uuid, 1);
313 CERROR("can't add initial connection\n");
314 GOTO(err_import, rc);
317 cli->cl_import = imp;
318 cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
319 cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
320 cli->cl_sandev = to_kdev_t(0);
322 if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
323 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
324 CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
325 name, obddev->obd_name,
326 imp->imp_target_uuid.uuid);
327 imp->imp_invalid = 1;
329 if (LUSTRE_CFG_BUFLEN(lcfg, 4) > 0)
330 mgmt_name = lustre_cfg_string(lcfg, 4);
332 mgmt_name = lustre_cfg_string(lcfg, 3);
336 if (mgmt_name != NULL) {
337 /* Register with management client if we need to. */
338 CDEBUG(D_HA, "%s registering with %s for events about %s\n",
339 obddev->obd_name, mgmt_name, server_uuid.uuid);
341 mgmt_obd = class_name2obd(mgmt_name);
343 CERROR("can't find mgmtcli %s to register\n",
345 GOTO(err_import, rc = -ENOSYS);
348 register_f = (mgmtcli_register_for_events_t)symbol_get("mgmtcli_register_for_events");
350 CERROR("can't i_m_g mgmtcli_register_for_events\n");
351 GOTO(err_import, rc = -ENOSYS);
354 rc = register_f(mgmt_obd, obddev, &imp->imp_target_uuid);
355 symbol_put("mgmtcli_register_for_events");
358 cli->cl_mgmtcli_obd = mgmt_obd;
364 class_destroy_import(imp);
372 int client_obd_cleanup(struct obd_device *obddev, int flags)
374 struct client_obd *cli = &obddev->u.cli;
380 if (cli->cl_mgmtcli_obd) {
381 mgmtcli_deregister_for_events_t dereg_f;
383 dereg_f = (mgmtcli_deregister_for_events_t)symbol_get("mgmtcli_deregister_for_events");
384 dereg_f(cli->cl_mgmtcli_obd, obddev);
385 symbol_put("mgmtcli_deregister_for_events");
388 if (cli->cl_write_gaps) {
389 CWARN("%s: [writes num: %lu, reads num: %lu]: %lu write gaps: %lu "
390 "av. (usec), %lu total (usec), %lu rpcs falled back to sync\n",
391 obddev->obd_name, cli->cl_write_num, cli->cl_read_num,
392 cli->cl_write_gaps, cli->cl_write_gap_sum / cli->cl_write_gaps,
393 cli->cl_write_gap_sum, cli->cl_sync_rpcs);
396 if (cli->cl_cache_wait_num) {
397 CWARN("%s: [cache waits num: %lu]: cache wait av. %lu (usec)\n",
398 obddev->obd_name, cli->cl_cache_wait_num,
399 cli->cl_cache_wait_sum / cli->cl_cache_wait_num);
402 if (cli->cl_dirty_av) {
403 CWARN("%s: pipe loading av. %lu (b), max pipe room %lu (b), pipe "
404 "loading av. ratio %lu%%, pipe dirty max %lu (b), pipe dirty "
405 "min %lu (b), pipe loading max ratio %lu%%\n", obddev->obd_name,
406 cli->cl_dirty_av, cli->cl_dirty_max,
407 (cli->cl_dirty_av * 100) / cli->cl_dirty_max,
408 cli->cl_dirty_dmax, cli->cl_dirty_dmin,
409 (cli->cl_dirty_dmax * 100) / cli->cl_dirty_max);
412 /* Here we try to drop the security structure after destroy import,
413 * to avoid issue of "sleep in spinlock".
415 class_import_get(cli->cl_import);
416 class_destroy_import(cli->cl_import);
417 ptlrpcs_import_drop_sec(cli->cl_import);
418 class_import_put(cli->cl_import);
419 cli->cl_import = NULL;
421 ldlm_put_ref(flags & OBD_OPT_FORCE);
425 int client_connect_import(struct lustre_handle *dlm_handle,
426 struct obd_device *obd,
427 struct obd_uuid *cluuid,
428 struct obd_connect_data *conn_data,
429 unsigned long connect_flags)
431 struct client_obd *cli = &obd->u.cli;
432 struct obd_import *imp = cli->cl_import;
433 struct obd_export *exp;
438 rc = class_connect(dlm_handle, obd, cluuid);
442 cli->cl_conn_count++;
443 if (cli->cl_conn_count > 1)
445 exp = class_conn2export(dlm_handle);
447 if (obd->obd_namespace != NULL)
448 CERROR("already have namespace!\n");
449 obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
450 LDLM_NAMESPACE_CLIENT);
451 if (obd->obd_namespace == NULL)
452 GOTO(out_disco, rc = -ENOMEM);
454 rc = ptlrpcs_import_get_sec(imp);
458 imp->imp_dlm_handle = *dlm_handle;
459 rc = ptlrpc_init_import(imp);
463 imp->imp_connect_flags = connect_flags;
465 memcpy(&imp->imp_connect_data, conn_data, sizeof(*conn_data));
467 rc = ptlrpc_connect_import(imp, NULL);
469 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
472 LASSERT(exp->exp_connection);
473 ptlrpc_pinger_add_import(imp);
478 ldlm_namespace_free(obd->obd_namespace, 0);
479 obd->obd_namespace = NULL;
481 cli->cl_conn_count--;
482 class_disconnect(exp, 0);
484 class_export_put(exp);
491 int client_disconnect_export(struct obd_export *exp, unsigned long flags)
493 struct obd_device *obd = class_exp2obd(exp);
494 struct client_obd *cli = &obd->u.cli;
495 struct obd_import *imp = cli->cl_import;
500 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
501 exp, exp ? exp->exp_handle.h_cookie : -1);
506 if (!cli->cl_conn_count) {
507 CERROR("disconnecting disconnected device (%s)\n",
509 GOTO(out_sem, rc = -EINVAL);
512 cli->cl_conn_count--;
513 if (cli->cl_conn_count)
514 GOTO(out_no_disconnect, rc = 0);
516 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
517 * delete it regardless. (It's safe to delete an import that was
519 (void)ptlrpc_pinger_del_import(imp);
521 if (obd->obd_namespace != NULL) {
522 /* obd_no_recov == local only */
523 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
524 obd->obd_no_recov, NULL);
525 ldlm_namespace_free(obd->obd_namespace, obd->obd_no_recov);
526 obd->obd_namespace = NULL;
530 * Yeah, obd_no_recov also (mainly) means "forced shutdown".
532 if (obd->obd_no_recov)
533 ptlrpc_invalidate_import(imp, 0);
535 rc = ptlrpc_disconnect_import(imp);
539 err = class_disconnect(exp, 0);
547 /* --------------------------------------------------------------------------
548 * from old lib/target.c
549 * -------------------------------------------------------------------------- */
551 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
552 struct obd_uuid *cluuid, int initial_conn)
554 if (exp->exp_connection && !initial_conn) {
555 struct lustre_handle *hdl;
556 hdl = &exp->exp_imp_reverse->imp_remote_handle;
557 /* Might be a re-connect after a partition. */
558 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
559 CERROR("%s reconnecting\n", cluuid->uuid);
560 conn->cookie = exp->exp_handle.h_cookie;
563 CERROR("%s reconnecting from %s, "
564 "handle mismatch (ours "LPX64", theirs "
565 LPX64")\n", cluuid->uuid,
566 exp->exp_connection->c_remote_uuid.uuid,
567 hdl->cookie, conn->cookie);
568 memset(conn, 0, sizeof *conn);
573 conn->cookie = exp->exp_handle.h_cookie;
574 CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
576 CDEBUG(D_IOCTL,"connect: cookie "LPX64"\n", conn->cookie);
580 static inline int ptlrpc_peer_is_local(struct ptlrpc_peer *peer)
582 ptl_process_id_t myid;
584 PtlGetId(peer->peer_ni->pni_ni_h, &myid);
585 return (memcmp(&peer->peer_id, &myid, sizeof(myid)) == 0);
588 /* To check whether the p_flavor is in deny list or not
591 * EPERM found, refuse
594 static int check_deny_list(struct list_head *head, __u32 flavor)
596 deny_sec_t *p_deny_sec = NULL;
597 deny_sec_t *n_deny_sec = NULL;
599 list_for_each_entry_safe(p_deny_sec, n_deny_sec, head, list) {
600 if (p_deny_sec->flavor == flavor)
606 int target_check_deny_sec(struct obd_device *target, struct ptlrpc_request *req)
611 flavor = req->rq_req_secflvr;
613 if (!strcmp(target->obd_type->typ_name, OBD_MDS_DEVICENAME)) {
614 spin_lock(&target->u.mds.mds_denylist_lock);
615 rc = check_deny_list(&target->u.mds.mds_denylist, flavor);
616 spin_unlock(&target->u.mds.mds_denylist_lock);
617 } else if (!strcmp(target->obd_type->typ_name, OBD_FILTER_DEVICENAME)) {
618 spin_lock(&target->u.filter.fo_denylist_lock);
619 rc = check_deny_list(&target->u.filter.fo_denylist, flavor);
620 spin_unlock(&target->u.filter.fo_denylist_lock);
626 int target_handle_connect(struct ptlrpc_request *req)
628 unsigned long connect_flags = 0, *cfp;
629 struct obd_device *target;
630 struct obd_export *export = NULL;
631 struct obd_import *revimp;
632 struct lustre_handle conn;
633 struct obd_uuid tgtuuid;
634 struct obd_uuid cluuid;
635 struct obd_uuid remote_uuid;
637 struct obd_connect_data *conn_data;
638 int conn_data_size = sizeof(*conn_data);
642 int initial_conn = 0;
643 char peer_str[PTL_NALFMT_SIZE];
644 const int offset = 1;
647 OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
649 LASSERT_REQSWAB (req, offset + 0);
650 str = lustre_msg_string(req->rq_reqmsg, offset + 0,
651 sizeof(tgtuuid) - 1);
653 CERROR("bad target UUID for connect\n");
654 GOTO(out, rc = -EINVAL);
657 obd_str2uuid (&tgtuuid, str);
658 target = class_uuid2obd(&tgtuuid);
660 target = class_name2obd(str);
662 if (!target || target->obd_stopping || !target->obd_set_up) {
663 CERROR("UUID '%s' is not available for connect from %s\n",
664 str, req->rq_peerstr);
665 GOTO(out, rc = -ENODEV);
668 /* check the secure deny list of mds/ost */
669 rc = target_check_deny_sec(target, req);
673 LASSERT_REQSWAB (req, offset + 1);
674 str = lustre_msg_string(req->rq_reqmsg, offset + 1, sizeof(cluuid) - 1);
676 CERROR("bad client UUID for connect\n");
677 GOTO(out, rc = -EINVAL);
680 obd_str2uuid (&cluuid, str);
682 /* XXX extract a nettype and format accordingly */
683 switch (sizeof(ptl_nid_t)) {
684 /* NB the casts only avoid compiler warnings */
686 snprintf((char *)remote_uuid.uuid, sizeof(remote_uuid),
687 "NET_"LPX64"_UUID", (__u64)req->rq_peer.peer_id.nid);
690 snprintf((char *)remote_uuid.uuid, sizeof(remote_uuid),
691 "NET_%x_UUID", (__u32)req->rq_peer.peer_id.nid);
697 tmp = lustre_msg_buf(req->rq_reqmsg, offset + 2, sizeof(conn));
699 GOTO(out, rc = -EPROTO);
701 memcpy(&conn, tmp, sizeof conn);
703 cfp = lustre_msg_buf(req->rq_reqmsg, offset + 3, sizeof(unsigned long));
704 LASSERT(cfp != NULL);
705 connect_flags = *cfp;
707 conn_data = lustre_swab_reqbuf(req, offset + 4, sizeof(*conn_data),
708 lustre_swab_connect);
710 GOTO(out, rc = -EPROTO);
712 rc = lustre_pack_reply(req, 1, &conn_data_size, NULL);
716 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_INITIAL)
719 /* lctl gets a backstage, all-access pass. */
720 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
721 goto dont_check_exports;
723 spin_lock(&target->obd_dev_lock);
724 list_for_each(p, &target->obd_exports) {
725 export = list_entry(p, struct obd_export, exp_obd_chain);
726 if (obd_uuid_equals(&cluuid, &export->exp_client_uuid)) {
727 spin_unlock(&target->obd_dev_lock);
728 LASSERT(export->exp_obd == target);
730 rc = target_handle_reconnect(&conn, export, &cluuid,
736 /* If we found an export, we already unlocked. */
738 spin_unlock(&target->obd_dev_lock);
739 } else if (req->rq_export == NULL &&
740 atomic_read(&export->exp_rpc_count) > 0) {
741 CWARN("%s: refuse connection from %s/%s to 0x%p/%d\n",
742 target->obd_name, cluuid.uuid,
743 ptlrpc_peernid2str(&req->rq_peer, peer_str),
744 export, atomic_read(&export->exp_refcount));
745 GOTO(out, rc = -EBUSY);
746 } else if (req->rq_export != NULL &&
747 atomic_read(&export->exp_rpc_count) > 1) {
748 CWARN("%s: refuse reconnection from %s@%s to 0x%p/%d\n",
749 target->obd_name, cluuid.uuid,
750 ptlrpc_peernid2str(&req->rq_peer, peer_str),
751 export, atomic_read(&export->exp_rpc_count));
752 GOTO(out, rc = -EBUSY);
753 } else if (req->rq_reqmsg->conn_cnt == 1 && !initial_conn) {
754 CERROR("%s reconnected with 1 conn_cnt; cookies not random?\n",
756 GOTO(out, rc = -EALREADY);
759 /* Tell the client if we're in recovery. */
760 /* If this is the first client, start the recovery timer */
761 CWARN("%s: connection from %s@%s/%lu %st"LPU64"\n", target->obd_name,
762 cluuid.uuid, ptlrpc_peernid2str(&req->rq_peer, peer_str), *cfp,
763 target->obd_recovering ? "recovering/" : "", conn_data->transno);
765 if (target->obd_recovering) {
766 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
767 target_start_recovery_timer(target);
771 /* Tell the client if we support replayable requests */
772 if (target->obd_replayable)
773 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
776 if (export == NULL) {
777 if (target->obd_recovering) {
778 CERROR("%s denying connection for new client %s@%s: "
779 "%d clients in recovery for %lds\n", target->obd_name,
781 ptlrpc_peernid2str(&req->rq_peer, peer_str),
782 target->obd_recoverable_clients,
783 (target->obd_recovery_timer.expires-jiffies)/HZ);
787 rc = obd_connect(&conn, target, &cluuid, conn_data,
792 /* Return only the parts of obd_connect_data that we understand, so the
793 * client knows that we don't understand the rest. */
794 conn_data->ocd_connect_flags &= OBD_CONNECT_SUPPORTED;
795 memcpy(lustre_msg_buf(req->rq_repmsg, 0, sizeof(*conn_data)), conn_data,
798 /* Tell the client if we support replayable requests */
799 if (target->obd_replayable)
800 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
802 /* If all else goes well, this is our RPC return code. */
805 if (rc && rc != EALREADY)
808 req->rq_repmsg->handle = conn;
810 /* If the client and the server are the same node, we will already
811 * have an export that really points to the client's DLM export,
812 * because we have a shared handles table.
814 * XXX this will go away when shaver stops sending the "connect" handle
815 * in the real "remote handle" field of the request --phik 24 Apr 2003
817 if (req->rq_export != NULL)
818 class_export_put(req->rq_export);
820 /* ownership of this export ref transfers to the request */
821 export = req->rq_export = class_conn2export(&conn);
822 LASSERT(export != NULL);
824 spin_lock_irqsave(&export->exp_lock, flags);
826 req->rq_repmsg->conn_cnt = export->exp_conn_cnt + 1;
827 } else if (export->exp_conn_cnt >= req->rq_reqmsg->conn_cnt) {
828 CERROR("%s@%s: already connected at a higher conn_cnt: %d > %d\n",
829 cluuid.uuid, ptlrpc_peernid2str(&req->rq_peer, peer_str),
830 export->exp_conn_cnt,
831 req->rq_reqmsg->conn_cnt);
832 spin_unlock_irqrestore(&export->exp_lock, flags);
833 GOTO(out, rc = -EALREADY);
835 export->exp_conn_cnt = req->rq_reqmsg->conn_cnt;
836 spin_unlock_irqrestore(&export->exp_lock, flags);
838 /* request from liblustre? */
839 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT)
840 export->exp_libclient = 1;
842 if (!(lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_ASYNC) &&
843 ptlrpc_peer_is_local(&req->rq_peer)) {
844 CWARN("%s: exp %p set sync\n", target->obd_name, export);
845 export->exp_sync = 1;
847 CDEBUG(D_HA, "%s: exp %p set async\n",target->obd_name,export);
848 export->exp_sync = 0;
851 if (export->exp_connection != NULL)
852 ptlrpc_put_connection(export->exp_connection);
853 export->exp_connection = ptlrpc_get_connection(&req->rq_peer,
856 if (rc == EALREADY) {
857 /* We indicate the reconnection in a flag, not an error code. */
858 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
862 spin_lock_bh(&target->obd_processing_task_lock);
863 if (target->obd_recovering && export->exp_connected == 0) {
864 __u64 t = conn_data->transno;
865 export->exp_connected = 1;
866 if ((lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_TRANSNO)
867 && t < target->obd_next_recovery_transno)
868 target->obd_next_recovery_transno = t;
869 target->obd_connected_clients++;
870 if (target->obd_connected_clients == target->obd_max_recoverable_clients)
871 wake_up(&target->obd_next_transno_waitq);
873 spin_unlock_bh(&target->obd_processing_task_lock);
875 memcpy(&conn, lustre_msg_buf(req->rq_reqmsg, offset + 2, sizeof(conn)),
878 if (export->exp_imp_reverse != NULL) {
879 /* same logic as client_obd_cleanup */
880 class_import_get(export->exp_imp_reverse);
881 class_destroy_import(export->exp_imp_reverse);
882 ptlrpcs_import_drop_sec(export->exp_imp_reverse);
883 class_import_put(export->exp_imp_reverse);
886 /* for the rest part, we return -ENOTCONN in case of errors
887 * in order to let client initialize connection again.
889 revimp = export->exp_imp_reverse = class_new_import();
891 CERROR("fail to alloc new reverse import.\n");
892 GOTO(out, rc = -ENOTCONN);
895 revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
896 revimp->imp_client = &export->exp_obd->obd_ldlm_client;
897 revimp->imp_remote_handle = conn;
898 revimp->imp_obd = target;
899 revimp->imp_dlm_fake = 1;
900 revimp->imp_state = LUSTRE_IMP_FULL;
902 rc = ptlrpcs_import_get_sec(revimp);
904 CERROR("reverse import can not get sec: %d\n", rc);
905 class_destroy_import(revimp);
906 export->exp_imp_reverse = NULL;
907 GOTO(out, rc = -ENOTCONN);
910 class_import_put(revimp);
912 rc = obd_connect_post(export, initial_conn, connect_flags);
919 int target_handle_disconnect(struct ptlrpc_request *req)
921 struct obd_export *exp;
925 rc = lustre_pack_reply(req, 0, NULL, NULL);
929 /* keep the rq_export around so we can send the reply */
930 exp = class_export_get(req->rq_export);
931 req->rq_status = obd_disconnect(exp, 0);
935 void target_destroy_export(struct obd_export *exp)
937 /* exports created from last_rcvd data, and "fake"
938 exports created by lctl don't have an import */
939 if (exp->exp_imp_reverse != NULL) {
940 ptlrpcs_import_drop_sec(exp->exp_imp_reverse);
941 class_destroy_import(exp->exp_imp_reverse);
944 /* We cancel locks at disconnect time, but this will catch any locks
945 * granted in a race with recovery-induced disconnect. */
946 if (exp->exp_obd->obd_namespace != NULL)
947 ldlm_cancel_locks_for_export(exp);
954 struct ptlrpc_request *
955 ptlrpc_clone_req( struct ptlrpc_request *orig_req)
957 struct ptlrpc_request *copy_req;
958 struct lustre_msg *copy_reqmsg;
960 OBD_ALLOC(copy_req, sizeof *copy_req);
963 OBD_ALLOC(copy_reqmsg, orig_req->rq_reqlen);
965 OBD_FREE(copy_req, sizeof *copy_req);
969 memcpy(copy_req, orig_req, sizeof *copy_req);
970 memcpy(copy_reqmsg, orig_req->rq_reqmsg, orig_req->rq_reqlen);
971 /* the copied req takes over the reply state and security data */
972 orig_req->rq_reply_state = NULL;
973 orig_req->rq_svcsec_data = NULL;
975 copy_req->rq_reqmsg = copy_reqmsg;
976 class_export_get(copy_req->rq_export);
977 INIT_LIST_HEAD(©_req->rq_list);
982 void ptlrpc_free_clone( struct ptlrpc_request *req)
985 svcsec_cleanup_req(req);
987 class_export_put(req->rq_export);
988 list_del(&req->rq_list);
989 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
990 OBD_FREE(req, sizeof *req);
993 static void target_release_saved_req(struct ptlrpc_request *req)
996 svcsec_cleanup_req(req);
998 class_export_put(req->rq_export);
999 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
1000 OBD_FREE(req, sizeof *req);
1003 static void target_finish_recovery(struct obd_device *obd)
1007 ldlm_reprocess_all_ns(obd->obd_namespace);
1009 /* when recovery finished, cleanup orphans on mds and ost */
1010 if (OBT(obd) && OBP(obd, postrecov)) {
1011 rc = OBP(obd, postrecov)(obd);
1013 CWARN("%s: all clients recovered, %d MDS "
1014 "orphans deleted\n", obd->obd_name, rc);
1016 CERROR("postrecov failed %d\n", rc);
1019 obd->obd_recovery_end = LTIME_S(CURRENT_TIME);
1023 static void abort_req_replay_queue(struct obd_device *obd)
1025 struct ptlrpc_request *req;
1026 struct list_head *tmp, *n;
1029 list_for_each_safe(tmp, n, &obd->obd_req_replay_queue) {
1030 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1031 list_del(&req->rq_list);
1032 DEBUG_REQ(D_ERROR, req, "aborted:");
1033 req->rq_status = -ENOTCONN;
1034 req->rq_type = PTL_RPC_MSG_ERR;
1035 rc = lustre_pack_reply(req, 0, NULL, NULL);
1039 DEBUG_REQ(D_ERROR, req,
1040 "packing failed for abort-reply; skipping");
1042 target_release_saved_req(req);
1046 static void abort_lock_replay_queue(struct obd_device *obd)
1048 struct ptlrpc_request *req;
1049 struct list_head *tmp, *n;
1052 list_for_each_safe(tmp, n, &obd->obd_lock_replay_queue) {
1053 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1054 list_del(&req->rq_list);
1055 DEBUG_REQ(D_ERROR, req, "aborted:");
1056 req->rq_status = -ENOTCONN;
1057 req->rq_type = PTL_RPC_MSG_ERR;
1058 rc = lustre_pack_reply(req, 0, NULL, NULL);
1062 DEBUG_REQ(D_ERROR, req,
1063 "packing failed for abort-reply; skipping");
1065 target_release_saved_req(req);
1069 /* Called from a cleanup function if the device is being cleaned up
1070 forcefully. The exports should all have been disconnected already,
1071 the only thing left to do is
1072 - clear the recovery flags
1074 - free queued requests and replies, but don't send replies
1075 Because the obd_stopping flag is set, no new requests should be received.
1078 void target_cleanup_recovery(struct obd_device *obd)
1080 struct list_head *tmp, *n;
1081 struct ptlrpc_request *req;
1083 spin_lock_bh(&obd->obd_processing_task_lock);
1084 if (!obd->obd_recovering) {
1085 spin_unlock_bh(&obd->obd_processing_task_lock);
1089 obd->obd_recovering = obd->obd_abort_recovery = 0;
1090 target_cancel_recovery_timer(obd);
1091 spin_unlock_bh(&obd->obd_processing_task_lock);
1093 list_for_each_safe(tmp, n, &obd->obd_req_replay_queue) {
1094 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1095 list_del(&req->rq_list);
1096 LASSERT (req->rq_reply_state == 0);
1097 target_release_saved_req(req);
1099 list_for_each_safe(tmp, n, &obd->obd_lock_replay_queue) {
1100 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1101 list_del(&req->rq_list);
1102 LASSERT (req->rq_reply_state == 0);
1103 target_release_saved_req(req);
1105 list_for_each_safe(tmp, n, &obd->obd_final_req_queue) {
1106 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1107 list_del(&req->rq_list);
1108 LASSERT (req->rq_reply_state == 0);
1109 target_release_saved_req(req);
1114 static void target_abort_recovery(void *data)
1116 struct obd_device *obd = data;
1118 LASSERT(!obd->obd_recovering);
1120 class_disconnect_stale_exports(obd, 0);
1122 CERROR("%s: recovery period over; disconnecting unfinished clients.\n",
1125 abort_recovery_queue(obd);
1126 target_finish_recovery(obd);
1127 ptlrpc_run_recovery_over_upcall(obd);
1131 static void target_recovery_expired(unsigned long castmeharder)
1133 struct obd_device *obd = (struct obd_device *)castmeharder;
1134 spin_lock_bh(&obd->obd_processing_task_lock);
1135 if (obd->obd_recovering)
1136 obd->obd_abort_recovery = 1;
1138 wake_up(&obd->obd_next_transno_waitq);
1139 spin_unlock_bh(&obd->obd_processing_task_lock);
1143 /* obd_processing_task_lock should be held */
1144 void target_cancel_recovery_timer(struct obd_device *obd)
1146 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
1147 del_timer(&obd->obd_recovery_timer);
1151 static void reset_recovery_timer(struct obd_device *obd)
1153 spin_lock_bh(&obd->obd_processing_task_lock);
1154 if (!obd->obd_recovering) {
1155 spin_unlock_bh(&obd->obd_processing_task_lock);
1158 CDEBUG(D_HA, "timer will expire in %u seconds\n",
1159 OBD_RECOVERY_TIMEOUT / HZ);
1160 mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
1161 spin_unlock_bh(&obd->obd_processing_task_lock);
1165 /* Only start it the first time called */
1166 void target_start_recovery_timer(struct obd_device *obd)
1168 spin_lock_bh(&obd->obd_processing_task_lock);
1169 if (!obd->obd_recovering || timer_pending(&obd->obd_recovery_timer)) {
1170 spin_unlock_bh(&obd->obd_processing_task_lock);
1173 CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
1174 OBD_RECOVERY_TIMEOUT / HZ);
1175 obd->obd_recovery_timer.function = target_recovery_expired;
1176 obd->obd_recovery_timer.data = (unsigned long)obd;
1177 mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
1178 spin_unlock_bh(&obd->obd_processing_task_lock);
1182 static int check_for_next_transno(struct obd_device *obd)
1184 struct ptlrpc_request *req = NULL;
1185 int wake_up = 0, connected, completed, queue_len, max;
1186 __u64 next_transno, req_transno;
1188 spin_lock_bh(&obd->obd_processing_task_lock);
1189 if (!list_empty(&obd->obd_req_replay_queue)) {
1190 req = list_entry(obd->obd_req_replay_queue.next,
1191 struct ptlrpc_request, rq_list);
1192 req_transno = req->rq_reqmsg->transno;
1197 max = obd->obd_max_recoverable_clients;
1198 connected = obd->obd_connected_clients;
1199 completed = max - obd->obd_recoverable_clients;
1200 queue_len = obd->obd_requests_queued_for_recovery;
1201 next_transno = obd->obd_next_recovery_transno;
1203 CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
1204 "req_transno: "LPU64", next_transno: "LPU64"\n",
1205 max, connected, completed, queue_len, req_transno, next_transno);
1206 if (obd->obd_abort_recovery) {
1207 CDEBUG(D_HA, "waking for aborted recovery\n");
1209 } else if (atomic_read(&obd->obd_req_replay_clients) == 0) {
1210 CDEBUG(D_HA, "waking for completed recovery\n");
1212 } else if (req_transno == next_transno) {
1213 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1215 } else if (queue_len + completed == max) {
1216 LASSERT(req->rq_reqmsg->transno >= next_transno);
1217 CDEBUG(req_transno > obd->obd_last_committed ? D_ERROR : D_HA,
1218 "waking for skipped transno (skip: "LPD64
1219 ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
1220 next_transno, queue_len, completed, max, req_transno);
1221 obd->obd_next_recovery_transno = req_transno;
1223 } else if (queue_len == atomic_read(&obd->obd_req_replay_clients)) {
1224 /* some clients haven't connected in time, but we can try
1225 * to replay requests that demand on already committed ones
1226 * also, we can replay first non-committed transation */
1227 LASSERT(req_transno != 0);
1228 if (req_transno == obd->obd_last_committed + 1) {
1229 obd->obd_next_recovery_transno = req_transno;
1230 } else if (req_transno > obd->obd_last_committed) {
1231 /* can't continue recovery: have no needed transno */
1232 obd->obd_abort_recovery = 1;
1233 CDEBUG(D_ERROR, "abort due to missed clients. max: %d, "
1234 "connected: %d, completed: %d, queue_len: %d, "
1235 "req_transno: "LPU64", next_transno: "LPU64"\n",
1236 max, connected, completed, queue_len,
1237 req_transno, next_transno);
1241 spin_unlock_bh(&obd->obd_processing_task_lock);
1246 static struct ptlrpc_request *
1247 target_next_replay_req(struct obd_device *obd)
1249 struct l_wait_info lwi = { 0 };
1250 struct ptlrpc_request *req;
1252 CDEBUG(D_HA, "Waiting for transno "LPD64"\n",
1253 obd->obd_next_recovery_transno);
1254 l_wait_event(obd->obd_next_transno_waitq,
1255 check_for_next_transno(obd), &lwi);
1257 spin_lock_bh(&obd->obd_processing_task_lock);
1258 if (obd->obd_abort_recovery) {
1260 } else if (!list_empty(&obd->obd_req_replay_queue)) {
1261 req = list_entry(obd->obd_req_replay_queue.next,
1262 struct ptlrpc_request, rq_list);
1263 list_del_init(&req->rq_list);
1264 obd->obd_requests_queued_for_recovery--;
1268 spin_unlock_bh(&obd->obd_processing_task_lock);
1272 static int check_for_next_lock(struct obd_device *obd)
1274 struct ptlrpc_request *req = NULL;
1277 spin_lock_bh(&obd->obd_processing_task_lock);
1278 if (!list_empty(&obd->obd_lock_replay_queue)) {
1279 req = list_entry(obd->obd_lock_replay_queue.next,
1280 struct ptlrpc_request, rq_list);
1281 CDEBUG(D_HA, "waking for next lock\n");
1283 } else if (atomic_read(&obd->obd_lock_replay_clients) == 0) {
1284 CDEBUG(D_HA, "waking for completed lock replay\n");
1286 } else if (obd->obd_abort_recovery) {
1287 CDEBUG(D_HA, "waking for aborted recovery\n");
1290 spin_unlock_bh(&obd->obd_processing_task_lock);
1295 static struct ptlrpc_request *
1296 target_next_replay_lock(struct obd_device *obd)
1298 struct l_wait_info lwi = { 0 };
1299 struct ptlrpc_request *req;
1301 CDEBUG(D_HA, "Waiting for lock\n");
1302 l_wait_event(obd->obd_next_transno_waitq,
1303 check_for_next_lock(obd), &lwi);
1305 spin_lock_bh(&obd->obd_processing_task_lock);
1306 if (obd->obd_abort_recovery) {
1308 } else if (!list_empty(&obd->obd_lock_replay_queue)) {
1309 req = list_entry(obd->obd_lock_replay_queue.next,
1310 struct ptlrpc_request, rq_list);
1311 list_del_init(&req->rq_list);
1315 spin_unlock_bh(&obd->obd_processing_task_lock);
1319 static struct ptlrpc_request *
1320 target_next_final_ping(struct obd_device *obd)
1322 struct ptlrpc_request *req;
1324 spin_lock_bh(&obd->obd_processing_task_lock);
1325 if (!list_empty(&obd->obd_final_req_queue)) {
1326 req = list_entry(obd->obd_final_req_queue.next,
1327 struct ptlrpc_request, rq_list);
1328 list_del_init(&req->rq_list);
1332 spin_unlock_bh(&obd->obd_processing_task_lock);
1336 static int req_replay_done(struct obd_export *exp)
1338 if (exp->exp_req_replay_needed)
1343 static int lock_replay_done(struct obd_export *exp)
1345 if (exp->exp_lock_replay_needed)
1350 static int connect_done(struct obd_export *exp)
1352 if (exp->exp_connected)
1357 static int check_for_clients(struct obd_device *obd)
1359 if (obd->obd_abort_recovery)
1361 LASSERT(obd->obd_connected_clients <= obd->obd_max_recoverable_clients);
1362 if (obd->obd_connected_clients == obd->obd_max_recoverable_clients)
1367 static int target_recovery_thread(void *arg)
1369 struct obd_device *obd = arg;
1370 struct ptlrpc_request *req;
1371 struct target_recovery_data *trd = &obd->obd_recovery_data;
1372 char peer_str[PTL_NALFMT_SIZE];
1373 struct l_wait_info lwi = { 0 };
1374 unsigned long delta;
1375 unsigned long flags;
1378 kportal_daemonize("tgt-recov");
1380 SIGNAL_MASK_LOCK(current, flags);
1381 sigfillset(¤t->blocked);
1383 SIGNAL_MASK_UNLOCK(current, flags);
1385 CERROR("%s: started recovery thread pid %d\n", obd->obd_name,
1387 trd->trd_processing_task = current->pid;
1389 obd->obd_recovering = 1;
1390 complete(&trd->trd_starting);
1392 /* first of all, we have to know the first transno to replay */
1393 obd->obd_abort_recovery = 0;
1394 l_wait_event(obd->obd_next_transno_waitq,
1395 check_for_clients(obd), &lwi);
1397 spin_lock_bh(&obd->obd_processing_task_lock);
1398 target_cancel_recovery_timer(obd);
1399 spin_unlock_bh(&obd->obd_processing_task_lock);
1401 /* If some clients haven't connected in time, evict them */
1402 if (obd->obd_abort_recovery) {
1404 CDEBUG(D_ERROR, "few clients haven't connect in time (%d/%d),"
1405 "evict them ...\n", obd->obd_connected_clients,
1406 obd->obd_max_recoverable_clients);
1407 obd->obd_abort_recovery = 0;
1408 stale = class_disconnect_stale_exports(obd, connect_done, 0);
1409 atomic_sub(stale, &obd->obd_req_replay_clients);
1410 atomic_sub(stale, &obd->obd_lock_replay_clients);
1413 /* next stage: replay requests */
1415 obd->obd_req_replaying = 1;
1416 CDEBUG(D_ERROR, "1: request replay stage - %d clients from t"LPU64"\n",
1417 atomic_read(&obd->obd_req_replay_clients),
1418 obd->obd_next_recovery_transno);
1419 while ((req = target_next_replay_req(obd))) {
1420 LASSERT(trd->trd_processing_task == current->pid);
1421 DEBUG_REQ(D_HA, req, "processing t"LPD64" from %s",
1422 req->rq_reqmsg->transno,
1423 ptlrpc_peernid2str(&req->rq_peer, peer_str));
1424 (void)trd->trd_recovery_handler(req);
1425 obd->obd_replayed_requests++;
1426 reset_recovery_timer(obd);
1427 /* bug 1580: decide how to properly sync() in recovery*/
1428 //mds_fsync_super(mds->mds_sb);
1429 ptlrpc_free_clone(req);
1430 spin_lock_bh(&obd->obd_processing_task_lock);
1431 obd->obd_next_recovery_transno++;
1432 spin_unlock_bh(&obd->obd_processing_task_lock);
1435 spin_lock_bh(&obd->obd_processing_task_lock);
1436 target_cancel_recovery_timer(obd);
1437 spin_unlock_bh(&obd->obd_processing_task_lock);
1439 /* If some clients haven't replayed requests in time, evict them */
1440 if (obd->obd_abort_recovery) {
1442 CDEBUG(D_ERROR, "req replay timed out, aborting ...\n");
1443 obd->obd_abort_recovery = 0;
1444 stale = class_disconnect_stale_exports(obd, req_replay_done, 0);
1445 atomic_sub(stale, &obd->obd_lock_replay_clients);
1446 abort_req_replay_queue(obd);
1447 /* XXX for debuggin tests 11 and 17 */
1451 /* The second stage: replay locks */
1452 CDEBUG(D_ERROR, "2: lock replay stage - %d clients\n",
1453 atomic_read(&obd->obd_lock_replay_clients));
1454 while ((req = target_next_replay_lock(obd))) {
1455 LASSERT(trd->trd_processing_task == current->pid);
1456 DEBUG_REQ(D_HA, req, "processing lock from %s: ",
1457 ptlrpc_peernid2str(&req->rq_peer, peer_str));
1458 (void)trd->trd_recovery_handler(req);
1459 reset_recovery_timer(obd);
1460 ptlrpc_free_clone(req);
1461 obd->obd_replayed_locks++;
1464 spin_lock_bh(&obd->obd_processing_task_lock);
1465 target_cancel_recovery_timer(obd);
1466 spin_unlock_bh(&obd->obd_processing_task_lock);
1468 /* If some clients haven't replayed requests in time, evict them */
1469 if (obd->obd_abort_recovery) {
1471 CERROR("lock replay timed out, aborting ...\n");
1472 obd->obd_abort_recovery = 0;
1473 stale = class_disconnect_stale_exports(obd, lock_replay_done, 0);
1474 abort_lock_replay_queue(obd);
1477 /* We drop recoverying flag to forward all new requests
1478 * to regular mds_handle() since now */
1479 spin_lock_bh(&obd->obd_processing_task_lock);
1480 obd->obd_recovering = 0;
1481 spin_unlock_bh(&obd->obd_processing_task_lock);
1483 /* The third stage: reply on final pings */
1484 CDEBUG(D_ERROR, "3: final stage - process recovery completion pings\n");
1485 while ((req = target_next_final_ping(obd))) {
1486 LASSERT(trd->trd_processing_task == current->pid);
1487 DEBUG_REQ(D_HA, req, "processing final ping from %s: ",
1488 ptlrpc_peernid2str(&req->rq_peer, peer_str));
1489 (void)trd->trd_recovery_handler(req);
1490 ptlrpc_free_clone(req);
1493 delta = (jiffies - delta) / HZ;
1494 CDEBUG(D_ERROR,"4: recovery completed in %lus - %d/%d reqs/locks\n",
1495 delta, obd->obd_replayed_requests, obd->obd_replayed_locks);
1496 if (delta > obd_timeout * 2) {
1497 CWARN("too long recovery - read logs\n");
1498 portals_debug_dumplog();
1500 target_finish_recovery(obd);
1502 trd->trd_processing_task = 0;
1503 complete(&trd->trd_finishing);
1507 int target_start_recovery_thread(struct obd_device *obd, svc_handler_t handler)
1510 struct target_recovery_data *trd = &obd->obd_recovery_data;
1512 memset(trd, 0, sizeof(*trd));
1513 init_completion(&trd->trd_starting);
1514 init_completion(&trd->trd_finishing);
1515 trd->trd_recovery_handler = handler;
1517 if (kernel_thread(target_recovery_thread, obd, 0) > 0) {
1518 wait_for_completion(&trd->trd_starting);
1519 LASSERT(obd->obd_recovering != 0);
1526 void target_stop_recovery_thread(struct obd_device *obd)
1528 spin_lock_bh(&obd->obd_processing_task_lock);
1529 if (obd->obd_recovery_data.trd_processing_task > 0) {
1530 struct target_recovery_data *trd = &obd->obd_recovery_data;
1531 CERROR("%s: aborting recovery\n", obd->obd_name);
1532 obd->obd_abort_recovery = 1;
1533 wake_up(&obd->obd_next_transno_waitq);
1534 spin_unlock_bh(&obd->obd_processing_task_lock);
1535 wait_for_completion(&trd->trd_finishing);
1537 spin_unlock_bh(&obd->obd_processing_task_lock);
1542 int target_process_req_flags(struct obd_device *obd, struct ptlrpc_request *req)
1544 struct obd_export *exp = req->rq_export;
1545 LASSERT(exp != NULL);
1546 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
1547 /* client declares he's ready to replay locks */
1548 spin_lock_bh(&obd->obd_processing_task_lock);
1549 if (exp->exp_req_replay_needed) {
1550 LASSERT(atomic_read(&obd->obd_req_replay_clients) > 0);
1551 exp->exp_req_replay_needed = 0;
1552 atomic_dec(&obd->obd_req_replay_clients);
1553 obd->obd_recoverable_clients--;
1554 if (atomic_read(&obd->obd_req_replay_clients) == 0)
1555 CDEBUG(D_HA, "all clients have replayed reqs\n");
1556 wake_up(&obd->obd_next_transno_waitq);
1558 spin_unlock_bh(&obd->obd_processing_task_lock);
1560 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
1561 /* client declares he's ready to complete recovery
1562 * so, we put the request on th final queue */
1563 spin_lock_bh(&obd->obd_processing_task_lock);
1564 if (exp->exp_lock_replay_needed) {
1565 LASSERT(atomic_read(&obd->obd_lock_replay_clients) > 0);
1566 exp->exp_lock_replay_needed = 0;
1567 atomic_dec(&obd->obd_lock_replay_clients);
1568 if (atomic_read(&obd->obd_lock_replay_clients) == 0)
1569 CDEBUG(D_HA, "all clients have replayed locks\n");
1570 wake_up(&obd->obd_next_transno_waitq);
1572 spin_unlock_bh(&obd->obd_processing_task_lock);
1578 int target_queue_recovery_request(struct ptlrpc_request *req,
1579 struct obd_device *obd)
1581 struct list_head *tmp;
1583 __u64 transno = req->rq_reqmsg->transno;
1585 if (obd->obd_recovery_data.trd_processing_task == current->pid) {
1586 /* Processing the queue right now, don't re-add. */
1590 target_process_req_flags(obd, req);
1592 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
1593 /* client declares he's ready to complete recovery
1594 * so, we put the request on th final queue */
1595 req = ptlrpc_clone_req(req);
1598 DEBUG_REQ(D_HA, req, "queue final req");
1599 spin_lock_bh(&obd->obd_processing_task_lock);
1600 list_add_tail(&req->rq_list, &obd->obd_final_req_queue);
1601 spin_unlock_bh(&obd->obd_processing_task_lock);
1604 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
1605 /* client declares he's ready to replay locks */
1606 req = ptlrpc_clone_req(req);
1609 DEBUG_REQ(D_HA, req, "queue lock replay req");
1610 spin_lock_bh(&obd->obd_processing_task_lock);
1611 list_add_tail(&req->rq_list, &obd->obd_lock_replay_queue);
1612 spin_unlock_bh(&obd->obd_processing_task_lock);
1613 wake_up(&obd->obd_next_transno_waitq);
1618 /* CAVEAT EMPTOR: The incoming request message has been swabbed
1619 * (i.e. buflens etc are in my own byte order), but type-dependent
1620 * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1623 INIT_LIST_HEAD(&req->rq_list);
1624 DEBUG_REQ(D_HA, req, "not queueing");
1629 /* If we're processing the queue, we want don't want to queue this
1632 * Also, if this request has a transno less than the one we're waiting
1633 * for, we should process it now. It could (and currently always will)
1634 * be an open request for a descriptor that was opened some time ago.
1636 * Also, a resent, replayed request that has already been
1637 * handled will pass through here and be processed immediately.
1639 spin_lock_bh(&obd->obd_processing_task_lock);
1640 if (transno < obd->obd_next_recovery_transno && obd->obd_req_replaying) {
1641 /* Processing the queue right now, don't re-add. */
1642 LASSERT(list_empty(&req->rq_list));
1643 spin_unlock_bh(&obd->obd_processing_task_lock);
1646 spin_unlock_bh(&obd->obd_processing_task_lock);
1648 /* A resent, replayed request that is still on the queue; just drop it.
1649 The queued request will handle this. */
1650 if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT | MSG_REPLAY))
1651 == (MSG_RESENT | MSG_REPLAY)) {
1652 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1656 req = ptlrpc_clone_req(req);
1660 spin_lock_bh(&obd->obd_processing_task_lock);
1663 list_for_each(tmp, &obd->obd_req_replay_queue) {
1664 struct ptlrpc_request *reqiter =
1665 list_entry(tmp, struct ptlrpc_request, rq_list);
1667 if (reqiter->rq_reqmsg->transno > transno) {
1668 list_add_tail(&req->rq_list, &reqiter->rq_list);
1675 list_add_tail(&req->rq_list, &obd->obd_req_replay_queue);
1677 obd->obd_requests_queued_for_recovery++;
1678 wake_up(&obd->obd_next_transno_waitq);
1679 spin_unlock_bh(&obd->obd_processing_task_lock);
1683 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1685 return req->rq_export->exp_obd;
1689 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1691 if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1692 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1693 DEBUG_REQ(D_ERROR, req, "dropping reply");
1694 /* NB this does _not_ send with ACK disabled, to simulate
1695 * sending OK, but timing out for the ACK */
1696 if (req->rq_reply_state != NULL) {
1697 if (!req->rq_reply_state->rs_difficult) {
1698 lustre_free_reply_state (req->rq_reply_state);
1699 req->rq_reply_state = NULL;
1701 struct ptlrpc_service *svc =
1702 req->rq_rqbd->rqbd_srv_ni->sni_service;
1703 atomic_inc(&svc->srv_outstanding_replies);
1710 req->rq_status = rc;
1711 return (ptlrpc_error(req));
1713 DEBUG_REQ(D_NET, req, "sending reply");
1716 return (ptlrpc_send_reply(req, 1));
1720 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1723 unsigned long flags;
1724 struct ptlrpc_reply_state *rs;
1725 struct obd_device *obd;
1726 struct obd_export *exp;
1727 struct ptlrpc_srv_ni *sni;
1728 struct ptlrpc_service *svc;
1730 sni = req->rq_rqbd->rqbd_srv_ni;
1731 svc = sni->sni_service;
1733 rs = req->rq_reply_state;
1734 if (rs == NULL || !rs->rs_difficult) {
1735 /* The easy case; no notifiers and reply_out_callback()
1736 * cleans up (i.e. we can't look inside rs after a
1737 * successful send) */
1738 netrc = target_send_reply_msg (req, rc, fail_id);
1740 LASSERT (netrc == 0 || req->rq_reply_state == NULL);
1744 /* must be an export if locks saved */
1745 LASSERT (req->rq_export != NULL);
1746 /* req/reply consistent */
1747 LASSERT (rs->rs_srv_ni == sni);
1750 LASSERT (!rs->rs_scheduled);
1751 LASSERT (!rs->rs_scheduled_ever);
1752 LASSERT (!rs->rs_handled);
1753 LASSERT (!rs->rs_on_net);
1754 LASSERT (rs->rs_export == NULL);
1755 LASSERT (list_empty(&rs->rs_obd_list));
1756 LASSERT (list_empty(&rs->rs_exp_list));
1758 exp = class_export_get (req->rq_export);
1761 /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1762 rs->rs_scheduled = 1;
1764 rs->rs_xid = req->rq_xid;
1765 rs->rs_transno = req->rq_transno;
1766 rs->rs_export = exp;
1768 spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
1770 if (rs->rs_transno > obd->obd_last_committed) {
1771 /* not committed already */
1772 list_add_tail (&rs->rs_obd_list,
1773 &obd->obd_uncommitted_replies);
1776 spin_unlock (&obd->obd_uncommitted_replies_lock);
1777 spin_lock (&exp->exp_lock);
1779 list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1781 spin_unlock_irqrestore (&exp->exp_lock, flags);
1783 netrc = target_send_reply_msg (req, rc, fail_id);
1785 spin_lock_irqsave (&svc->srv_lock, flags);
1787 svc->srv_n_difficult_replies++;
1789 if (netrc != 0) /* error sending: reply is off the net */
1792 if (!rs->rs_on_net || /* some notifier */
1793 list_empty(&rs->rs_exp_list) || /* completed already */
1794 list_empty(&rs->rs_obd_list)) {
1795 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1796 wake_up (&svc->srv_waitq);
1798 list_add (&rs->rs_list, &sni->sni_active_replies);
1799 rs->rs_scheduled = 0; /* allow notifier to schedule */
1802 spin_unlock_irqrestore (&svc->srv_lock, flags);
1805 int target_handle_ping(struct ptlrpc_request *req)
1807 return lustre_pack_reply(req, 0, NULL, NULL);