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 the Lustre file system, http://www.lustre.org
7 * Lustre is a trademark of Cluster File Systems, Inc.
9 * You may have signed or agreed to another license before downloading
10 * this software. If so, you are bound by the terms and conditions
11 * of that agreement, and the following does not apply to you. See the
12 * LICENSE file included with this distribution for more information.
14 * If you did not agree to a different license, then this copy of Lustre
15 * is open source software; you can redistribute it and/or modify it
16 * under the terms of version 2 of the GNU General Public License as
17 * published by the Free Software Foundation.
19 * In either case, Lustre is distributed in the hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * license text for more details.
26 # define EXPORT_SYMTAB
28 #define DEBUG_SUBSYSTEM S_LDLM
31 # include <libcfs/libcfs.h>
33 # include <liblustre.h>
36 #include <lustre_mds.h>
37 #include <lustre_dlm.h>
38 #include <lustre_net.h>
39 #include <lustre_sec.h>
41 /* @priority: if non-zero, move the selected to the list head
42 * @create: if zero, only search in existed connections
44 static int import_set_conn(struct obd_import *imp, struct obd_uuid *uuid,
45 int priority, int create)
47 struct ptlrpc_connection *ptlrpc_conn;
48 struct obd_import_conn *imp_conn = NULL, *item;
52 if (!create && !priority) {
53 CDEBUG(D_HA, "Nothing to do\n");
57 ptlrpc_conn = ptlrpc_uuid_to_connection(uuid);
59 CDEBUG(D_HA, "can't find connection %s\n", uuid->uuid);
64 OBD_ALLOC(imp_conn, sizeof(*imp_conn));
66 GOTO(out_put, rc = -ENOMEM);
70 spin_lock(&imp->imp_lock);
71 list_for_each_entry(item, &imp->imp_conn_list, oic_item) {
72 if (obd_uuid_equals(uuid, &item->oic_uuid)) {
74 list_del(&item->oic_item);
75 list_add(&item->oic_item, &imp->imp_conn_list);
76 item->oic_last_attempt = 0;
78 CDEBUG(D_HA, "imp %p@%s: found existing conn %s%s\n",
79 imp, imp->imp_obd->obd_name, uuid->uuid,
80 (priority ? ", moved to head" : ""));
81 spin_unlock(&imp->imp_lock);
82 GOTO(out_free, rc = 0);
87 imp_conn->oic_conn = ptlrpc_conn;
88 imp_conn->oic_uuid = *uuid;
89 item->oic_last_attempt = 0;
91 list_add(&imp_conn->oic_item, &imp->imp_conn_list);
93 list_add_tail(&imp_conn->oic_item, &imp->imp_conn_list);
94 CDEBUG(D_HA, "imp %p@%s: add connection %s at %s\n",
95 imp, imp->imp_obd->obd_name, uuid->uuid,
96 (priority ? "head" : "tail"));
98 spin_unlock(&imp->imp_lock);
99 GOTO(out_free, rc = -ENOENT);
102 spin_unlock(&imp->imp_lock);
106 OBD_FREE(imp_conn, sizeof(*imp_conn));
108 ptlrpc_put_connection(ptlrpc_conn);
112 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid)
114 return import_set_conn(imp, uuid, 1, 0);
117 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
120 return import_set_conn(imp, uuid, priority, 1);
123 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
125 struct obd_import_conn *imp_conn;
126 struct obd_export *dlmexp;
130 spin_lock(&imp->imp_lock);
131 if (list_empty(&imp->imp_conn_list)) {
132 LASSERT(!imp->imp_connection);
136 list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
137 if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
139 LASSERT(imp_conn->oic_conn);
141 /* is current conn? */
142 if (imp_conn == imp->imp_conn_current) {
143 LASSERT(imp_conn->oic_conn == imp->imp_connection);
145 if (imp->imp_state != LUSTRE_IMP_CLOSED &&
146 imp->imp_state != LUSTRE_IMP_DISCON) {
147 CERROR("can't remove current connection\n");
148 GOTO(out, rc = -EBUSY);
151 ptlrpc_put_connection(imp->imp_connection);
152 imp->imp_connection = NULL;
154 dlmexp = class_conn2export(&imp->imp_dlm_handle);
155 if (dlmexp && dlmexp->exp_connection) {
156 LASSERT(dlmexp->exp_connection ==
158 ptlrpc_put_connection(dlmexp->exp_connection);
159 dlmexp->exp_connection = NULL;
163 list_del(&imp_conn->oic_item);
164 ptlrpc_put_connection(imp_conn->oic_conn);
165 OBD_FREE(imp_conn, sizeof(*imp_conn));
166 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
167 imp, imp->imp_obd->obd_name, uuid->uuid);
172 spin_unlock(&imp->imp_lock);
174 CERROR("connection %s not found\n", uuid->uuid);
178 static void destroy_import(struct obd_import *imp)
180 /* drop security policy instance after all rpc finished/aborted
181 * to let all busy credentials be released.
183 class_import_get(imp);
184 class_destroy_import(imp);
185 sptlrpc_import_put_sec(imp);
186 class_import_put(imp);
189 /* configure an RPC client OBD device
194 * 3 - inactive-on-startup
196 int client_obd_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
198 struct client_obd *cli = &obddev->u.cli;
199 struct obd_import *imp;
200 struct obd_uuid server_uuid;
201 int rq_portal, rp_portal, connect_op;
202 char *name = obddev->obd_type->typ_name;
206 /* In a more perfect world, we would hang a ptlrpc_client off of
207 * obd_type and just use the values from there. */
208 if (!strcmp(name, LUSTRE_OSC_NAME)) {
209 rq_portal = OST_REQUEST_PORTAL;
210 rp_portal = OSC_REPLY_PORTAL;
211 connect_op = OST_CONNECT;
212 } else if (!strcmp(name, LUSTRE_MDC_NAME)) {
213 rq_portal = MDS_REQUEST_PORTAL;
214 rp_portal = MDC_REPLY_PORTAL;
215 connect_op = MDS_CONNECT;
216 } else if (!strcmp(name, LUSTRE_MGC_NAME)) {
217 rq_portal = MGS_REQUEST_PORTAL;
218 rp_portal = MGC_REPLY_PORTAL;
219 connect_op = MGS_CONNECT;
221 CERROR("unknown client OBD type \"%s\", can't setup\n",
226 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
227 CERROR("requires a TARGET UUID\n");
231 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
232 CERROR("client UUID must be less than 38 characters\n");
236 if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
237 CERROR("setup requires a SERVER UUID\n");
241 if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
242 CERROR("target UUID must be less than 38 characters\n");
246 sema_init(&cli->cl_sem, 1);
247 sema_init(&cli->cl_mgc_sem, 1);
248 cli->cl_sec_conf.sfc_rpc_flavor = SPTLRPC_FLVR_NULL;
249 cli->cl_sec_conf.sfc_bulk_csum = BULK_CSUM_ALG_NULL;
250 cli->cl_sec_conf.sfc_bulk_priv = BULK_PRIV_ALG_NULL;
251 cli->cl_sec_conf.sfc_flags = 0;
252 cli->cl_conn_count = 0;
253 memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
254 min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
255 sizeof(server_uuid)));
258 cli->cl_avail_grant = 0;
259 /* FIXME: should limit this for the sum of all cl_dirty_max */
260 cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
261 if (cli->cl_dirty_max >> CFS_PAGE_SHIFT > num_physpages / 8)
262 cli->cl_dirty_max = num_physpages << (CFS_PAGE_SHIFT - 3);
263 CFS_INIT_LIST_HEAD(&cli->cl_cache_waiters);
264 CFS_INIT_LIST_HEAD(&cli->cl_loi_ready_list);
265 CFS_INIT_LIST_HEAD(&cli->cl_loi_write_list);
266 CFS_INIT_LIST_HEAD(&cli->cl_loi_read_list);
267 client_obd_list_lock_init(&cli->cl_loi_list_lock);
268 cli->cl_r_in_flight = 0;
269 cli->cl_w_in_flight = 0;
271 spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
272 spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
273 spin_lock_init(&cli->cl_read_page_hist.oh_lock);
274 spin_lock_init(&cli->cl_write_page_hist.oh_lock);
275 spin_lock_init(&cli->cl_read_offset_hist.oh_lock);
276 spin_lock_init(&cli->cl_write_offset_hist.oh_lock);
277 #ifdef ENABLE_CHECKSUM
278 cli->cl_checksum = 1;
281 /* This value may be changed at connect time in
282 ptlrpc_connect_interpret. */
283 cli->cl_max_pages_per_rpc = min((int)PTLRPC_MAX_BRW_PAGES,
284 (int)(1024 * 1024 >> CFS_PAGE_SHIFT));
286 if (!strcmp(name, LUSTRE_MDC_NAME)) {
287 cli->cl_max_rpcs_in_flight = MDC_MAX_RIF_DEFAULT;
288 } else if (num_physpages >> (20 - CFS_PAGE_SHIFT) <= 128 /* MB */) {
289 cli->cl_max_rpcs_in_flight = 2;
290 } else if (num_physpages >> (20 - CFS_PAGE_SHIFT) <= 256 /* MB */) {
291 cli->cl_max_rpcs_in_flight = 3;
292 } else if (num_physpages >> (20 - CFS_PAGE_SHIFT) <= 512 /* MB */) {
293 cli->cl_max_rpcs_in_flight = 4;
295 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
298 rc = ldlm_get_ref(LDLM_NAMESPACE_CLIENT);
300 CERROR("ldlm_get_ref failed: %d\n", rc);
304 ptlrpc_init_client(rq_portal, rp_portal, name,
305 &obddev->obd_ldlm_client);
307 imp = class_new_import(obddev);
309 GOTO(err_ldlm, rc = -ENOENT);
310 imp->imp_client = &obddev->obd_ldlm_client;
311 imp->imp_connect_op = connect_op;
312 imp->imp_initial_recov = 1;
313 imp->imp_initial_recov_bk = 0;
314 CFS_INIT_LIST_HEAD(&imp->imp_pinger_chain);
315 memcpy(cli->cl_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
316 LUSTRE_CFG_BUFLEN(lcfg, 1));
317 class_import_put(imp);
319 rc = client_import_add_conn(imp, &server_uuid, 1);
321 CERROR("can't add initial connection\n");
322 GOTO(err_import, rc);
325 cli->cl_import = imp;
326 /* cli->cl_max_mds_{easize,cookiesize} updated by mdc_init_ea_size() */
327 cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
328 cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
330 if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
331 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
332 CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
333 name, obddev->obd_name,
334 cli->cl_target_uuid.uuid);
335 spin_lock(&imp->imp_lock);
336 imp->imp_invalid = 1;
337 spin_unlock(&imp->imp_lock);
341 cli->cl_qchk_stat = CL_NOT_QUOTACHECKED;
346 class_destroy_import(imp);
348 ldlm_put_ref(LDLM_NAMESPACE_CLIENT, 0);
354 int client_obd_cleanup(struct obd_device *obddev)
357 ldlm_put_ref(LDLM_NAMESPACE_CLIENT, obddev->obd_force);
361 /* ->o_connect() method for client side (OSC and MDC and MGC) */
362 int client_connect_import(const struct lu_env *env,
363 struct lustre_handle *dlm_handle,
364 struct obd_device *obd, struct obd_uuid *cluuid,
365 struct obd_connect_data *data)
367 struct client_obd *cli = &obd->u.cli;
368 struct obd_import *imp = cli->cl_import;
369 struct obd_export *exp;
370 struct obd_connect_data *ocd;
374 mutex_down(&cli->cl_sem);
375 rc = class_connect(dlm_handle, obd, cluuid);
379 cli->cl_conn_count++;
380 if (cli->cl_conn_count > 1)
382 exp = class_conn2export(dlm_handle);
384 if (obd->obd_namespace != NULL)
385 CERROR("already have namespace!\n");
386 obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
387 LDLM_NAMESPACE_CLIENT,
388 LDLM_NAMESPACE_GREEDY);
389 if (obd->obd_namespace == NULL)
390 GOTO(out_disco, rc = -ENOMEM);
392 imp->imp_dlm_handle = *dlm_handle;
393 rc = ptlrpc_init_import(imp);
397 rc = sptlrpc_import_get_sec(imp, NULL, cli->cl_sec_conf.sfc_rpc_flavor,
398 cli->cl_sec_conf.sfc_flags);
402 ocd = &imp->imp_connect_data;
405 imp->imp_connect_flags_orig = data->ocd_connect_flags;
408 rc = ptlrpc_connect_import(imp, NULL);
410 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
413 LASSERT(exp->exp_connection);
416 LASSERT((ocd->ocd_connect_flags & data->ocd_connect_flags) ==
417 ocd->ocd_connect_flags);
418 data->ocd_connect_flags = ocd->ocd_connect_flags;
421 ptlrpc_pinger_add_import(imp);
427 ldlm_namespace_free(obd->obd_namespace, 0);
428 obd->obd_namespace = NULL;
430 cli->cl_conn_count--;
431 class_disconnect(exp);
433 class_export_put(exp);
436 mutex_up(&cli->cl_sem);
440 int client_disconnect_export(struct obd_export *exp)
442 struct obd_device *obd = class_exp2obd(exp);
443 struct client_obd *cli;
444 struct obd_import *imp;
449 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
450 exp, exp ? exp->exp_handle.h_cookie : -1);
455 imp = cli->cl_import;
457 mutex_down(&cli->cl_sem);
458 if (!cli->cl_conn_count) {
459 CERROR("disconnecting disconnected device (%s)\n",
461 GOTO(out_sem, rc = -EINVAL);
464 cli->cl_conn_count--;
465 if (cli->cl_conn_count)
466 GOTO(out_no_disconnect, rc = 0);
468 /* Mark import deactivated now, so we don't try to reconnect if any
469 * of the cleanup RPCs fails (e.g. ldlm cancel, etc). We don't
470 * fully deactivate the import, or that would drop all requests. */
471 spin_lock(&imp->imp_lock);
472 imp->imp_deactive = 1;
473 spin_unlock(&imp->imp_lock);
475 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
476 * delete it regardless. (It's safe to delete an import that was
478 (void)ptlrpc_pinger_del_import(imp);
480 if (obd->obd_namespace != NULL) {
481 /* obd_force == local only */
482 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
483 obd->obd_force ? LDLM_FL_LOCAL_ONLY:0,
485 ldlm_namespace_free(obd->obd_namespace, obd->obd_force);
486 obd->obd_namespace = NULL;
490 rc = ptlrpc_disconnect_import(imp, 0);
492 ptlrpc_invalidate_import(imp);
493 ptlrpc_free_rq_pool(imp->imp_rq_pool);
495 cli->cl_import = NULL;
499 err = class_disconnect(exp);
503 mutex_up(&cli->cl_sem);
507 /* --------------------------------------------------------------------------
508 * from old lib/target.c
509 * -------------------------------------------------------------------------- */
511 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
512 struct obd_uuid *cluuid, int initial_conn)
515 if (exp->exp_connection && exp->exp_imp_reverse && !initial_conn) {
516 struct lustre_handle *hdl;
517 hdl = &exp->exp_imp_reverse->imp_remote_handle;
518 /* Might be a re-connect after a partition. */
519 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
520 CWARN("%s: %s reconnecting\n", exp->exp_obd->obd_name,
522 conn->cookie = exp->exp_handle.h_cookie;
523 /* target_handle_connect() treats EALREADY and
524 * -EALREADY differently. EALREADY means we are
525 * doing a valid reconnect from the same client. */
528 CERROR("%s reconnecting from %s, "
529 "handle mismatch (ours "LPX64", theirs "
530 LPX64")\n", cluuid->uuid,
531 exp->exp_connection->c_remote_uuid.uuid,
532 hdl->cookie, conn->cookie);
533 memset(conn, 0, sizeof *conn);
534 /* target_handle_connect() treats EALREADY and
535 * -EALREADY differently. -EALREADY is an error
536 * (same UUID, different handle). */
541 conn->cookie = exp->exp_handle.h_cookie;
542 CDEBUG(D_HA, "connect export for UUID '%s' at %p, cookie "LPX64"\n",
543 cluuid->uuid, exp, conn->cookie);
547 void target_client_add_cb(struct obd_device *obd, __u64 transno, void *cb_data,
550 struct obd_export *exp = cb_data;
552 CDEBUG(D_HA, "%s: committing for initial connect of %s\n",
553 obd->obd_name, exp->exp_client_uuid.uuid);
555 spin_lock(&exp->exp_lock);
556 exp->exp_need_sync = 0;
557 spin_unlock(&exp->exp_lock);
559 EXPORT_SYMBOL(target_client_add_cb);
561 int target_handle_connect(struct ptlrpc_request *req)
563 struct obd_device *target, *targref = NULL;
564 struct obd_export *export = NULL;
565 struct obd_import *revimp;
566 struct lustre_handle conn;
567 struct obd_uuid tgtuuid;
568 struct obd_uuid cluuid;
569 struct obd_uuid remote_uuid;
573 int initial_conn = 0;
574 struct obd_connect_data *data;
575 int size[2] = { sizeof(struct ptlrpc_body), sizeof(*data) };
578 OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
580 LASSERT_REQSWAB(req, REQ_REC_OFF);
581 str = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF, sizeof(tgtuuid)-1);
583 DEBUG_REQ(D_ERROR, req, "bad target UUID for connect");
584 GOTO(out, rc = -EINVAL);
587 obd_str2uuid (&tgtuuid, str);
588 target = class_uuid2obd(&tgtuuid);
590 target = class_name2obd(str);
592 if (!target || target->obd_stopping || !target->obd_set_up) {
593 LCONSOLE_ERROR_MSG(0x137, "UUID '%s' is not available "
594 " for connect (%s)\n", str,
595 !target ? "no target" :
596 (target->obd_stopping ? "stopping" :
598 GOTO(out, rc = -ENODEV);
601 if (target->obd_no_conn) {
602 LCONSOLE_WARN("%s: temporarily refusing client connection "
603 "from %s\n", target->obd_name,
604 libcfs_nid2str(req->rq_peer.nid));
605 GOTO(out, rc = -EAGAIN);
608 /* Make sure the target isn't cleaned up while we're here. Yes,
609 there's still a race between the above check and our incref here.
610 Really, class_uuid2obd should take the ref. */
611 targref = class_incref(target);
613 LASSERT_REQSWAB(req, REQ_REC_OFF + 1);
614 str = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF + 1,
617 DEBUG_REQ(D_ERROR, req, "bad client UUID for connect");
618 GOTO(out, rc = -EINVAL);
621 obd_str2uuid (&cluuid, str);
623 /* XXX extract a nettype and format accordingly */
624 switch (sizeof(lnet_nid_t)) {
625 /* NB the casts only avoid compiler warnings */
627 snprintf(remote_uuid.uuid, sizeof remote_uuid,
628 "NET_"LPX64"_UUID", (__u64)req->rq_peer.nid);
631 snprintf(remote_uuid.uuid, sizeof remote_uuid,
632 "NET_%x_UUID", (__u32)req->rq_peer.nid);
638 tmp = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 2, sizeof conn);
640 GOTO(out, rc = -EPROTO);
642 memcpy(&conn, tmp, sizeof conn);
644 data = lustre_swab_reqbuf(req, REQ_REC_OFF + 3, sizeof(*data),
645 lustre_swab_connect);
648 GOTO(out, rc = -EPROTO);
650 rc = lustre_pack_reply(req, 2, size, NULL);
654 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
656 DEBUG_REQ(D_WARNING, req, "Refusing old (unversioned) "
657 "libclient connection attempt");
658 GOTO(out, rc = -EPROTO);
659 } else if (data->ocd_version < LUSTRE_VERSION_CODE -
660 LUSTRE_VERSION_ALLOWED_OFFSET ||
661 data->ocd_version > LUSTRE_VERSION_CODE +
662 LUSTRE_VERSION_ALLOWED_OFFSET) {
663 DEBUG_REQ(D_WARNING, req, "Refusing %s (%d.%d.%d.%d) "
664 "libclient connection attempt",
665 data->ocd_version < LUSTRE_VERSION_CODE ?
667 OBD_OCD_VERSION_MAJOR(data->ocd_version),
668 OBD_OCD_VERSION_MINOR(data->ocd_version),
669 OBD_OCD_VERSION_PATCH(data->ocd_version),
670 OBD_OCD_VERSION_FIX(data->ocd_version));
671 data = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
672 offsetof(typeof(*data),
674 sizeof(data->ocd_version));
676 data->ocd_connect_flags = OBD_CONNECT_VERSION;
677 data->ocd_version = LUSTRE_VERSION_CODE;
679 GOTO(out, rc = -EPROTO);
683 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_INITIAL)
686 /* lctl gets a backstage, all-access pass. */
687 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
688 goto dont_check_exports;
690 spin_lock(&target->obd_dev_lock);
691 list_for_each(p, &target->obd_exports) {
692 export = list_entry(p, struct obd_export, exp_obd_chain);
693 if (obd_uuid_equals(&cluuid, &export->exp_client_uuid)) {
694 if (export->exp_connecting) { /* bug 9635, et. al. */
695 CWARN("%s: exp %p already connecting\n",
696 export->exp_obd->obd_name, export);
702 /* make darn sure this is coming from the same peer
703 * if the UUIDs matched */
704 if ((export->exp_connection != NULL) &&
705 (strcmp(libcfs_nid2str(req->rq_peer.nid),
706 libcfs_nid2str(export->exp_connection->c_peer.nid)))) {
707 CWARN("%s: cookie %s seen on new NID %s when "
708 "existing NID %s is already connected\n",
709 target->obd_name, cluuid.uuid,
710 libcfs_nid2str(req->rq_peer.nid),
711 libcfs_nid2str(export->exp_connection->c_peer.nid));
717 spin_lock(&export->exp_lock);
718 export->exp_connecting = 1;
719 spin_unlock(&export->exp_lock);
720 spin_unlock(&target->obd_dev_lock);
721 LASSERT(export->exp_obd == target);
723 rc = target_handle_reconnect(&conn, export, &cluuid,
730 /* If we found an export, we already unlocked. */
732 spin_unlock(&target->obd_dev_lock);
733 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_CONNECT, 2 * obd_timeout);
734 } else if (req->rq_export == NULL &&
735 atomic_read(&export->exp_rpc_count) > 0) {
736 CWARN("%s: refuse connection from %s/%s to 0x%p/%d\n",
737 target->obd_name, cluuid.uuid,
738 libcfs_nid2str(req->rq_peer.nid),
739 export, atomic_read(&export->exp_refcount));
740 GOTO(out, rc = -EBUSY);
741 } else if (req->rq_export != NULL &&
742 (atomic_read(&export->exp_rpc_count) > 1)) {
743 CWARN("%s: refuse reconnection from %s@%s to 0x%p/%d\n",
744 target->obd_name, cluuid.uuid,
745 libcfs_nid2str(req->rq_peer.nid),
746 export, atomic_read(&export->exp_rpc_count));
747 GOTO(out, rc = -EBUSY);
748 } else if (lustre_msg_get_conn_cnt(req->rq_reqmsg) == 1 &&
750 CERROR("%s: NID %s (%s) reconnected with 1 conn_cnt; "
751 "cookies not random?\n", target->obd_name,
752 libcfs_nid2str(req->rq_peer.nid), cluuid.uuid);
753 GOTO(out, rc = -EALREADY);
755 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_RECONNECT, 2 * obd_timeout);
756 if (req->rq_export == NULL && initial_conn)
757 export->exp_last_request_time =
758 max(export->exp_last_request_time,
759 (time_t)CURRENT_SECONDS);
762 /* We want to handle EALREADY but *not* -EALREADY from
763 * target_handle_reconnect(), return reconnection state in a flag */
764 if (rc == EALREADY) {
765 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
770 /* Tell the client if we're in recovery. */
771 /* If this is the first client, start the recovery timer */
772 CWARN("%s: connection from %s@%s %st"LPU64" exp %p cur %ld last %ld\n",
773 target->obd_name, cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
774 target->obd_recovering ? "recovering/" : "", data->ocd_transno,
775 export, (long)CURRENT_SECONDS,
776 export ? (long)export->exp_last_request_time : 0);
779 if (target->obd_recovering) {
780 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
781 target_start_recovery_timer(target);
784 /* Tell the client if we support replayable requests */
785 if (target->obd_replayable)
786 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
788 if (export == NULL) {
789 if (target->obd_recovering) {
790 CERROR("%s: denying connection for new client %s (%s): "
791 "%d clients in recovery for %lds\n",
793 libcfs_nid2str(req->rq_peer.nid), cluuid.uuid,
794 target->obd_recoverable_clients,
795 cfs_duration_sec(cfs_time_sub(cfs_timer_deadline(&target->obd_recovery_timer),
796 cfs_time_current())));
800 rc = obd_connect(req->rq_svc_thread->t_env,
801 &conn, target, &cluuid, data);
804 rc = obd_reconnect(export, target, &cluuid, data);
808 /* Return only the parts of obd_connect_data that we understand, so the
809 * client knows that we don't understand the rest. */
811 memcpy(lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
813 data, sizeof(*data));
815 /* If all else goes well, this is our RPC return code. */
818 lustre_msg_set_handle(req->rq_repmsg, &conn);
820 /* ownership of this export ref transfers to the request AFTER we
821 * drop any previous reference the request had, but we don't want
822 * that to go to zero before we get our new export reference. */
823 export = class_conn2export(&conn);
825 DEBUG_REQ(D_ERROR, req, "Missing export!");
826 GOTO(out, rc = -ENODEV);
829 /* If the client and the server are the same node, we will already
830 * have an export that really points to the client's DLM export,
831 * because we have a shared handles table.
833 * XXX this will go away when shaver stops sending the "connect" handle
834 * in the real "remote handle" field of the request --phik 24 Apr 2003
836 if (req->rq_export != NULL)
837 class_export_put(req->rq_export);
839 req->rq_export = export;
841 spin_lock(&export->exp_lock);
843 lustre_msg_set_conn_cnt(req->rq_repmsg, export->exp_conn_cnt + 1);
844 } else if (export->exp_conn_cnt >= lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
845 spin_unlock(&export->exp_lock);
846 CERROR("%s: %s already connected at higher conn_cnt: %d > %d\n",
847 cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
848 export->exp_conn_cnt,
849 lustre_msg_get_conn_cnt(req->rq_reqmsg));
851 GOTO(out, rc = -EALREADY);
853 export->exp_conn_cnt = lustre_msg_get_conn_cnt(req->rq_reqmsg);
855 /* request from liblustre? Don't evict it for not pinging. */
856 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
857 export->exp_libclient = 1;
858 spin_unlock(&export->exp_lock);
860 spin_lock(&target->obd_dev_lock);
861 list_del_init(&export->exp_obd_chain_timed);
862 spin_unlock(&target->obd_dev_lock);
864 spin_unlock(&export->exp_lock);
867 if (export->exp_connection != NULL)
868 ptlrpc_put_connection(export->exp_connection);
869 export->exp_connection = ptlrpc_get_connection(req->rq_peer,
873 spin_lock_bh(&target->obd_processing_task_lock);
874 if (target->obd_recovering && !export->exp_in_recovery) {
875 spin_lock(&export->exp_lock);
876 export->exp_in_recovery = 1;
877 export->exp_req_replay_needed = 1;
878 export->exp_lock_replay_needed = 1;
879 spin_unlock(&export->exp_lock);
880 if ((lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_TRANSNO)
881 && data->ocd_transno < target->obd_next_recovery_transno)
882 target->obd_next_recovery_transno = data->ocd_transno;
883 target->obd_connected_clients++;
884 /* each connected client is counted as recoverable */
885 target->obd_recoverable_clients++;
886 atomic_inc(&target->obd_req_replay_clients);
887 atomic_inc(&target->obd_lock_replay_clients);
888 if (target->obd_connected_clients ==
889 target->obd_max_recoverable_clients)
890 wake_up(&target->obd_next_transno_waitq);
892 spin_unlock_bh(&target->obd_processing_task_lock);
894 lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 2, sizeof conn),
897 if (export->exp_imp_reverse != NULL) {
898 /* destroyed import can be still referenced in ctxt */
899 obd_set_info_async(export, strlen(KEY_REVIMP_UPD),
900 KEY_REVIMP_UPD, 0, NULL, NULL);
901 destroy_import(export->exp_imp_reverse);
904 /* for the rest part, we return -ENOTCONN in case of errors
905 * in order to let client initialize connection again.
907 revimp = export->exp_imp_reverse = class_new_import(target);
909 CERROR("fail to alloc new reverse import.\n");
910 GOTO(out, rc = -ENOTCONN);
913 revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
914 revimp->imp_client = &export->exp_obd->obd_ldlm_client;
915 revimp->imp_remote_handle = conn;
916 revimp->imp_dlm_fake = 1;
917 revimp->imp_state = LUSTRE_IMP_FULL;
919 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_NEXT_VER) {
920 revimp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
921 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_NEXT_VER);
924 rc = sptlrpc_import_get_sec(revimp, req->rq_svc_ctx,
925 req->rq_sec_flavor, 0);
927 CERROR("Failed to get sec for reverse import: %d\n", rc);
928 export->exp_imp_reverse = NULL;
929 class_destroy_import(revimp);
932 class_import_put(revimp);
935 spin_lock(&export->exp_lock);
936 export->exp_connecting = 0;
937 spin_unlock(&export->exp_lock);
940 class_decref(targref);
946 int target_handle_disconnect(struct ptlrpc_request *req)
951 rc = lustre_pack_reply(req, 1, NULL, NULL);
955 /* keep the rq_export around so we can send the reply */
956 req->rq_status = obd_disconnect(class_export_get(req->rq_export));
961 void target_destroy_export(struct obd_export *exp)
963 /* exports created from last_rcvd data, and "fake"
964 exports created by lctl don't have an import */
965 if (exp->exp_imp_reverse != NULL)
966 destroy_import(exp->exp_imp_reverse);
968 /* We cancel locks at disconnect time, but this will catch any locks
969 * granted in a race with recovery-induced disconnect. */
970 if (exp->exp_obd->obd_namespace != NULL)
971 ldlm_cancel_locks_for_export(exp);
978 struct ptlrpc_request *ptlrpc_clone_req( struct ptlrpc_request *orig_req)
980 struct ptlrpc_request *copy_req;
981 struct lustre_msg *copy_reqmsg;
982 struct ptlrpc_user_desc *udesc = NULL;
984 OBD_ALLOC_PTR(copy_req);
987 OBD_ALLOC(copy_reqmsg, orig_req->rq_reqlen);
989 OBD_FREE_PTR(copy_req);
993 if (orig_req->rq_user_desc) {
994 int ngroups = orig_req->rq_user_desc->pud_ngroups;
996 OBD_ALLOC(udesc, sptlrpc_user_desc_size(ngroups));
998 OBD_FREE(copy_reqmsg, orig_req->rq_reqlen);
999 OBD_FREE_PTR(copy_req);
1002 memcpy(udesc, orig_req->rq_user_desc,
1003 sptlrpc_user_desc_size(ngroups));
1006 *copy_req = *orig_req;
1007 memcpy(copy_reqmsg, orig_req->rq_reqmsg, orig_req->rq_reqlen);
1008 copy_req->rq_reqmsg = copy_reqmsg;
1009 copy_req->rq_user_desc = udesc;
1011 class_export_get(copy_req->rq_export);
1012 CFS_INIT_LIST_HEAD(©_req->rq_list);
1013 sptlrpc_svc_ctx_addref(copy_req);
1015 if (copy_req->rq_reply_state) {
1016 /* the copied req takes over the reply state */
1017 orig_req->rq_reply_state = NULL;
1018 /* to catch further access */
1019 orig_req->rq_repmsg = NULL;
1020 orig_req->rq_replen = 0;
1026 void ptlrpc_free_clone( struct ptlrpc_request *req)
1028 if (req->rq_reply_state) {
1029 ptlrpc_rs_decref(req->rq_reply_state);
1030 req->rq_reply_state = NULL;
1033 sptlrpc_svc_ctx_decref(req);
1034 class_export_put(req->rq_export);
1035 list_del(&req->rq_list);
1037 if (req->rq_user_desc) {
1038 int ngroups = req->rq_user_desc->pud_ngroups;
1039 OBD_FREE(req->rq_user_desc, sptlrpc_user_desc_size(ngroups));
1041 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
1046 static void target_finish_recovery(struct obd_device *obd)
1049 LCONSOLE_INFO("%s: sending delayed replies to recovered clients\n",
1052 ldlm_reprocess_all_ns(obd->obd_namespace);
1054 /* when recovery finished, cleanup orphans on mds and ost */
1055 if (OBT(obd) && OBP(obd, postrecov)) {
1056 int rc = OBP(obd, postrecov)(obd);
1057 LCONSOLE_WARN("%s: recovery %s: rc %d\n", obd->obd_name,
1058 rc < 0 ? "failed" : "complete", rc);
1061 obd->obd_recovery_end = CURRENT_SECONDS;
1065 static void abort_req_replay_queue(struct obd_device *obd)
1067 struct ptlrpc_request *req, *n;
1069 list_for_each_entry_safe(req, n, &obd->obd_req_replay_queue, rq_list) {
1070 DEBUG_REQ(D_WARNING, req, "aborted:");
1071 req->rq_status = -ENOTCONN;
1072 if (ptlrpc_error(req)) {
1073 DEBUG_REQ(D_ERROR, req,
1074 "failed abort_req_reply; skipping");
1076 ptlrpc_free_clone(req);
1080 static void abort_lock_replay_queue(struct obd_device *obd)
1082 struct ptlrpc_request *req, *n;
1084 list_for_each_entry_safe(req, n, &obd->obd_lock_replay_queue, rq_list){
1085 DEBUG_REQ(D_ERROR, req, "aborted:");
1086 req->rq_status = -ENOTCONN;
1087 if (ptlrpc_error(req)) {
1088 DEBUG_REQ(D_ERROR, req,
1089 "failed abort_lock_reply; skipping");
1091 ptlrpc_free_clone(req);
1096 /* Called from a cleanup function if the device is being cleaned up
1097 forcefully. The exports should all have been disconnected already,
1098 the only thing left to do is
1099 - clear the recovery flags
1101 - free queued requests and replies, but don't send replies
1102 Because the obd_stopping flag is set, no new requests should be received.
1105 void target_cleanup_recovery(struct obd_device *obd)
1107 struct ptlrpc_request *req, *n;
1110 LASSERT(obd->obd_stopping);
1112 spin_lock_bh(&obd->obd_processing_task_lock);
1113 if (!obd->obd_recovering) {
1114 spin_unlock_bh(&obd->obd_processing_task_lock);
1118 obd->obd_recovering = obd->obd_abort_recovery = 0;
1119 target_cancel_recovery_timer(obd);
1120 spin_unlock_bh(&obd->obd_processing_task_lock);
1122 list_for_each_entry_safe(req, n, &obd->obd_req_replay_queue, rq_list) {
1123 LASSERT (req->rq_reply_state == 0);
1124 ptlrpc_free_clone(req);
1126 list_for_each_entry_safe(req, n, &obd->obd_lock_replay_queue, rq_list){
1127 LASSERT (req->rq_reply_state == 0);
1128 ptlrpc_free_clone(req);
1130 list_for_each_entry_safe(req, n, &obd->obd_final_req_queue, rq_list) {
1131 LASSERT (req->rq_reply_state == 0);
1132 ptlrpc_free_clone(req);
1138 static void target_recovery_expired(unsigned long castmeharder)
1140 struct obd_device *obd = (struct obd_device *)castmeharder;
1141 CERROR("%s: recovery timed out, aborting\n", obd->obd_name);
1142 spin_lock_bh(&obd->obd_processing_task_lock);
1143 if (obd->obd_recovering)
1144 obd->obd_abort_recovery = 1;
1145 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1146 spin_unlock_bh(&obd->obd_processing_task_lock);
1150 /* obd_processing_task_lock should be held */
1151 void target_cancel_recovery_timer(struct obd_device *obd)
1153 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
1154 cfs_timer_disarm(&obd->obd_recovery_timer);
1157 static void reset_recovery_timer(struct obd_device *obd)
1159 spin_lock_bh(&obd->obd_processing_task_lock);
1160 if (!obd->obd_recovering) {
1161 spin_unlock_bh(&obd->obd_processing_task_lock);
1164 cfs_timer_arm(&obd->obd_recovery_timer,
1165 cfs_time_shift(OBD_RECOVERY_TIMEOUT));
1166 spin_unlock_bh(&obd->obd_processing_task_lock);
1167 CDEBUG(D_HA, "%s: timer will expire in %u seconds\n", obd->obd_name,
1168 OBD_RECOVERY_TIMEOUT);
1169 /* Only used for lprocfs_status */
1170 obd->obd_recovery_end = CURRENT_SECONDS + OBD_RECOVERY_TIMEOUT;
1174 /* Only start it the first time called */
1175 void target_start_recovery_timer(struct obd_device *obd)
1177 spin_lock_bh(&obd->obd_processing_task_lock);
1178 if (obd->obd_recovery_handler
1179 || timer_pending((struct timer_list *)&obd->obd_recovery_timer)) {
1180 spin_unlock_bh(&obd->obd_processing_task_lock);
1183 CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
1184 OBD_RECOVERY_TIMEOUT);
1185 cfs_timer_init(&obd->obd_recovery_timer, target_recovery_expired, obd);
1186 spin_unlock_bh(&obd->obd_processing_task_lock);
1188 reset_recovery_timer(obd);
1192 static int check_for_next_transno(struct obd_device *obd)
1194 struct ptlrpc_request *req = NULL;
1195 int wake_up = 0, connected, completed, queue_len, max;
1196 __u64 next_transno, req_transno;
1198 spin_lock_bh(&obd->obd_processing_task_lock);
1200 if (!list_empty(&obd->obd_req_replay_queue)) {
1201 req = list_entry(obd->obd_req_replay_queue.next,
1202 struct ptlrpc_request, rq_list);
1203 req_transno = lustre_msg_get_transno(req->rq_reqmsg);
1208 max = obd->obd_max_recoverable_clients;
1209 connected = obd->obd_connected_clients;
1210 completed = connected - obd->obd_recoverable_clients;
1211 queue_len = obd->obd_requests_queued_for_recovery;
1212 next_transno = obd->obd_next_recovery_transno;
1214 CDEBUG(D_HA, "max: %d, connected: %d, completed: %d, queue_len: %d, "
1215 "req_transno: "LPU64", next_transno: "LPU64"\n",
1216 max, connected, completed, queue_len, req_transno, next_transno);
1218 if (obd->obd_abort_recovery) {
1219 CDEBUG(D_HA, "waking for aborted recovery\n");
1221 } else if (atomic_read(&obd->obd_req_replay_clients) == 0) {
1222 CDEBUG(D_HA, "waking for completed recovery\n");
1224 } else if (req_transno == next_transno) {
1225 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1227 } else if (queue_len + completed == max) {
1228 /* handle gaps occured due to lost reply. It is allowed gaps
1229 * because all clients are connected and there will be resend
1230 * for missed transaction */
1231 LASSERTF(req_transno >= next_transno,
1232 "req_transno: "LPU64", next_transno: "LPU64"\n",
1233 req_transno, next_transno);
1235 CDEBUG(req_transno > obd->obd_last_committed ? D_ERROR : D_HA,
1236 "waking for skipped transno (skip: "LPD64
1237 ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
1238 next_transno, queue_len, completed, connected, req_transno);
1239 obd->obd_next_recovery_transno = req_transno;
1241 } else if (queue_len == atomic_read(&obd->obd_req_replay_clients)) {
1242 /* some clients haven't connected in time, but we can try
1243 * to replay requests that demand on already committed ones
1244 * also, we can replay first non-committed transation */
1245 LASSERT(req_transno != 0);
1246 if (req_transno == obd->obd_last_committed + 1) {
1247 obd->obd_next_recovery_transno = req_transno;
1248 } else if (req_transno > obd->obd_last_committed) {
1249 /* can't continue recovery: have no needed transno */
1250 obd->obd_abort_recovery = 1;
1251 CDEBUG(D_ERROR, "abort due to missed clients. max: %d, "
1252 "connected: %d, completed: %d, queue_len: %d, "
1253 "req_transno: "LPU64", next_transno: "LPU64"\n",
1254 max, connected, completed, queue_len,
1255 req_transno, next_transno);
1260 spin_unlock_bh(&obd->obd_processing_task_lock);
1264 static struct ptlrpc_request *target_next_replay_req(struct obd_device *obd)
1266 struct l_wait_info lwi = { 0 };
1267 struct ptlrpc_request *req;
1269 CDEBUG(D_HA, "Waiting for transno "LPD64"\n",
1270 obd->obd_next_recovery_transno);
1271 l_wait_event(obd->obd_next_transno_waitq,
1272 check_for_next_transno(obd), &lwi);
1274 spin_lock_bh(&obd->obd_processing_task_lock);
1275 if (obd->obd_abort_recovery) {
1277 } else if (!list_empty(&obd->obd_req_replay_queue)) {
1278 req = list_entry(obd->obd_req_replay_queue.next,
1279 struct ptlrpc_request, rq_list);
1280 list_del_init(&req->rq_list);
1281 obd->obd_requests_queued_for_recovery--;
1285 spin_unlock_bh(&obd->obd_processing_task_lock);
1289 static int check_for_next_lock(struct obd_device *obd)
1291 struct ptlrpc_request *req = NULL;
1294 spin_lock_bh(&obd->obd_processing_task_lock);
1295 if (!list_empty(&obd->obd_lock_replay_queue)) {
1296 req = list_entry(obd->obd_lock_replay_queue.next,
1297 struct ptlrpc_request, rq_list);
1298 CDEBUG(D_HA, "waking for next lock\n");
1300 } else if (atomic_read(&obd->obd_lock_replay_clients) == 0) {
1301 CDEBUG(D_HA, "waking for completed lock replay\n");
1303 } else if (obd->obd_abort_recovery) {
1304 CDEBUG(D_HA, "waking for aborted recovery\n");
1307 spin_unlock_bh(&obd->obd_processing_task_lock);
1312 static struct ptlrpc_request *target_next_replay_lock(struct obd_device *obd)
1314 struct l_wait_info lwi = { 0 };
1315 struct ptlrpc_request *req;
1317 CDEBUG(D_HA, "Waiting for lock\n");
1318 l_wait_event(obd->obd_next_transno_waitq,
1319 check_for_next_lock(obd), &lwi);
1321 spin_lock_bh(&obd->obd_processing_task_lock);
1322 if (obd->obd_abort_recovery) {
1324 } else if (!list_empty(&obd->obd_lock_replay_queue)) {
1325 req = list_entry(obd->obd_lock_replay_queue.next,
1326 struct ptlrpc_request, rq_list);
1327 list_del_init(&req->rq_list);
1331 spin_unlock_bh(&obd->obd_processing_task_lock);
1335 static struct ptlrpc_request *target_next_final_ping(struct obd_device *obd)
1337 struct ptlrpc_request *req;
1339 spin_lock_bh(&obd->obd_processing_task_lock);
1340 if (!list_empty(&obd->obd_final_req_queue)) {
1341 req = list_entry(obd->obd_final_req_queue.next,
1342 struct ptlrpc_request, rq_list);
1343 list_del_init(&req->rq_list);
1347 spin_unlock_bh(&obd->obd_processing_task_lock);
1351 static inline int req_replay_done(struct obd_export *exp)
1353 return (exp->exp_req_replay_needed == 0);
1356 static inline int lock_replay_done(struct obd_export *exp)
1358 return (exp->exp_lock_replay_needed == 0);
1361 static inline int connect_done(struct obd_export *exp)
1363 return (exp->exp_in_recovery != 0);
1366 static int check_for_clients(struct obd_device *obd)
1368 if (obd->obd_abort_recovery)
1370 LASSERT(obd->obd_connected_clients <= obd->obd_max_recoverable_clients);
1371 if (obd->obd_no_conn == 0 &&
1372 obd->obd_connected_clients == obd->obd_max_recoverable_clients)
1377 static int handle_recovery_req(struct ptlrpc_thread *thread,
1378 struct ptlrpc_request *req,
1379 svc_handler_t handler)
1384 rc = lu_context_init(&req->rq_session, LCT_SESSION);
1386 CERROR("Failure to initialize session: %d\n", rc);
1389 req->rq_session.lc_thread = thread;
1390 lu_context_enter(&req->rq_session);
1391 req->rq_svc_thread = thread;
1392 req->rq_svc_thread->t_env->le_ses = &req->rq_session;
1396 lu_context_exit(&req->rq_session);
1397 lu_context_fini(&req->rq_session);
1398 /* don't reset timer for final stage */
1399 if (!req_replay_done(req->rq_export) ||
1400 !lock_replay_done(req->rq_export))
1401 reset_recovery_timer(class_exp2obd(req->rq_export));
1402 ptlrpc_free_clone(req);
1406 static int target_recovery_thread(void *arg)
1408 struct obd_device *obd = arg;
1409 struct ptlrpc_request *req;
1410 struct target_recovery_data *trd = &obd->obd_recovery_data;
1411 struct l_wait_info lwi = { 0 };
1412 unsigned long delta;
1413 unsigned long flags;
1415 struct ptlrpc_thread fake_svc_thread, *thread = &fake_svc_thread;
1416 __u32 recov_ctx_tags = LCT_MD_THREAD;
1420 cfs_daemonize("tgt_recov");
1422 SIGNAL_MASK_LOCK(current, flags);
1423 sigfillset(¤t->blocked);
1425 SIGNAL_MASK_UNLOCK(current, flags);
1427 rc = lu_context_init(&env.le_ctx, recov_ctx_tags);
1431 thread->t_env = &env;
1432 env.le_ctx.lc_thread = thread;
1434 CERROR("%s: started recovery thread pid %d\n", obd->obd_name,
1436 trd->trd_processing_task = current->pid;
1438 obd->obd_recovering = 1;
1439 complete(&trd->trd_starting);
1441 /* first of all, we have to know the first transno to replay */
1442 obd->obd_abort_recovery = 0;
1443 l_wait_event(obd->obd_next_transno_waitq,
1444 check_for_clients(obd), &lwi);
1446 spin_lock_bh(&obd->obd_processing_task_lock);
1447 target_cancel_recovery_timer(obd);
1448 spin_unlock_bh(&obd->obd_processing_task_lock);
1450 /* If some clients haven't connected in time, evict them */
1451 if (obd->obd_abort_recovery) {
1452 CWARN("Some clients haven't connect in time (%d/%d),"
1453 "evict them\n", obd->obd_connected_clients,
1454 obd->obd_max_recoverable_clients);
1455 obd->obd_abort_recovery = obd->obd_stopping;
1456 class_disconnect_stale_exports(obd, connect_done);
1458 /* next stage: replay requests */
1460 obd->obd_req_replaying = 1;
1461 CDEBUG(D_INFO, "1: request replay stage - %d clients from t"LPU64"\n",
1462 atomic_read(&obd->obd_req_replay_clients),
1463 obd->obd_next_recovery_transno);
1464 while ((req = target_next_replay_req(obd))) {
1465 LASSERT(trd->trd_processing_task == current->pid);
1466 DEBUG_REQ(D_HA, req, "processing t"LPD64" from %s",
1467 lustre_msg_get_transno(req->rq_reqmsg),
1468 libcfs_nid2str(req->rq_peer.nid));
1470 handle_recovery_req(thread, req,
1471 trd->trd_recovery_handler);
1472 obd->obd_replayed_requests++;
1473 spin_lock_bh(&obd->obd_processing_task_lock);
1474 obd->obd_next_recovery_transno++;
1475 spin_unlock_bh(&obd->obd_processing_task_lock);
1478 spin_lock_bh(&obd->obd_processing_task_lock);
1479 target_cancel_recovery_timer(obd);
1480 spin_unlock_bh(&obd->obd_processing_task_lock);
1482 /* If some clients haven't replayed requests in time, evict them */
1483 if (obd->obd_abort_recovery) {
1484 CDEBUG(D_ERROR, "req replay timed out, aborting ...\n");
1485 obd->obd_abort_recovery = obd->obd_stopping;
1486 class_disconnect_stale_exports(obd, req_replay_done);
1487 abort_req_replay_queue(obd);
1489 /* The second stage: replay locks */
1490 CDEBUG(D_INFO, "2: lock replay stage - %d clients\n",
1491 atomic_read(&obd->obd_lock_replay_clients));
1492 while ((req = target_next_replay_lock(obd))) {
1493 LASSERT(trd->trd_processing_task == current->pid);
1494 DEBUG_REQ(D_HA|D_WARNING, req, "processing lock from %s: ",
1495 libcfs_nid2str(req->rq_peer.nid));
1496 handle_recovery_req(thread, req,
1497 trd->trd_recovery_handler);
1498 obd->obd_replayed_locks++;
1501 spin_lock_bh(&obd->obd_processing_task_lock);
1502 target_cancel_recovery_timer(obd);
1503 spin_unlock_bh(&obd->obd_processing_task_lock);
1504 /* If some clients haven't replayed requests in time, evict them */
1505 if (obd->obd_abort_recovery) {
1507 CERROR("lock replay timed out, aborting ...\n");
1508 obd->obd_abort_recovery = obd->obd_stopping;
1509 stale = class_disconnect_stale_exports(obd, lock_replay_done);
1510 abort_lock_replay_queue(obd);
1513 /* We drop recoverying flag to forward all new requests
1514 * to regular mds_handle() since now */
1515 spin_lock_bh(&obd->obd_processing_task_lock);
1516 obd->obd_recovering = obd->obd_abort_recovery = 0;
1517 spin_unlock_bh(&obd->obd_processing_task_lock);
1518 /* The third stage: reply on final pings */
1519 CDEBUG(D_INFO, "3: final stage - process recovery completion pings\n");
1520 while ((req = target_next_final_ping(obd))) {
1521 LASSERT(trd->trd_processing_task == current->pid);
1522 DEBUG_REQ(D_HA, req, "processing final ping from %s: ",
1523 libcfs_nid2str(req->rq_peer.nid));
1524 handle_recovery_req(thread, req,
1525 trd->trd_recovery_handler);
1528 delta = (jiffies - delta) / HZ;
1529 CDEBUG(D_INFO,"4: recovery completed in %lus - %d/%d reqs/locks\n",
1530 delta, obd->obd_replayed_requests, obd->obd_replayed_locks);
1531 LASSERT(atomic_read(&obd->obd_req_replay_clients) == 0);
1532 LASSERT(atomic_read(&obd->obd_lock_replay_clients) == 0);
1533 if (delta > obd_timeout * 2) {
1534 CWARN("too long recovery - read logs\n");
1535 libcfs_debug_dumplog();
1538 target_finish_recovery(obd);
1541 trd->trd_processing_task = 0;
1542 complete(&trd->trd_finishing);
1546 int target_start_recovery_thread(struct obd_device *obd, svc_handler_t handler)
1549 struct target_recovery_data *trd = &obd->obd_recovery_data;
1551 memset(trd, 0, sizeof(*trd));
1552 init_completion(&trd->trd_starting);
1553 init_completion(&trd->trd_finishing);
1554 trd->trd_recovery_handler = handler;
1556 if (kernel_thread(target_recovery_thread, obd, 0) > 0) {
1557 wait_for_completion(&trd->trd_starting);
1558 LASSERT(obd->obd_recovering != 0);
1565 void target_stop_recovery_thread(struct obd_device *obd)
1567 spin_lock_bh(&obd->obd_processing_task_lock);
1568 if (obd->obd_recovery_data.trd_processing_task > 0) {
1569 struct target_recovery_data *trd = &obd->obd_recovery_data;
1570 CERROR("%s: Aborting recovery\n", obd->obd_name);
1571 obd->obd_abort_recovery = 1;
1572 wake_up(&obd->obd_next_transno_waitq);
1573 spin_unlock_bh(&obd->obd_processing_task_lock);
1574 wait_for_completion(&trd->trd_finishing);
1576 spin_unlock_bh(&obd->obd_processing_task_lock);
1580 void target_recovery_fini(struct obd_device *obd)
1582 class_disconnect_exports(obd);
1583 target_stop_recovery_thread(obd);
1584 target_cleanup_recovery(obd);
1586 EXPORT_SYMBOL(target_recovery_fini);
1588 void target_recovery_init(struct obd_device *obd, svc_handler_t handler)
1590 if (obd->obd_max_recoverable_clients == 0)
1593 CWARN("RECOVERY: service %s, %d recoverable clients, "
1594 "last_transno "LPU64"\n", obd->obd_name,
1595 obd->obd_max_recoverable_clients, obd->obd_last_committed);
1596 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
1597 target_start_recovery_thread(obd, handler);
1598 obd->obd_recovery_start = CURRENT_SECONDS;
1599 /* Only used for lprocfs_status */
1600 obd->obd_recovery_end = obd->obd_recovery_start + OBD_RECOVERY_TIMEOUT;
1602 EXPORT_SYMBOL(target_recovery_init);
1606 int target_process_req_flags(struct obd_device *obd, struct ptlrpc_request *req)
1608 struct obd_export *exp = req->rq_export;
1609 LASSERT(exp != NULL);
1610 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
1611 /* client declares he's ready to replay locks */
1612 spin_lock_bh(&obd->obd_processing_task_lock);
1613 if (exp->exp_req_replay_needed) {
1614 LASSERT(atomic_read(&obd->obd_req_replay_clients) > 0);
1615 spin_lock(&exp->exp_lock);
1616 exp->exp_req_replay_needed = 0;
1617 spin_unlock(&exp->exp_lock);
1618 atomic_dec(&obd->obd_req_replay_clients);
1619 LASSERT(obd->obd_recoverable_clients > 0);
1620 obd->obd_recoverable_clients--;
1621 if (atomic_read(&obd->obd_req_replay_clients) == 0)
1622 CDEBUG(D_HA, "all clients have replayed reqs\n");
1623 wake_up(&obd->obd_next_transno_waitq);
1625 spin_unlock_bh(&obd->obd_processing_task_lock);
1627 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
1628 /* client declares he's ready to complete recovery
1629 * so, we put the request on th final queue */
1630 spin_lock_bh(&obd->obd_processing_task_lock);
1631 if (exp->exp_lock_replay_needed) {
1632 LASSERT(atomic_read(&obd->obd_lock_replay_clients) > 0);
1633 spin_lock(&exp->exp_lock);
1634 exp->exp_lock_replay_needed = 0;
1635 spin_unlock(&exp->exp_lock);
1636 atomic_dec(&obd->obd_lock_replay_clients);
1637 if (atomic_read(&obd->obd_lock_replay_clients) == 0)
1638 CDEBUG(D_HA, "all clients have replayed locks\n");
1639 wake_up(&obd->obd_next_transno_waitq);
1641 spin_unlock_bh(&obd->obd_processing_task_lock);
1647 int target_queue_recovery_request(struct ptlrpc_request *req,
1648 struct obd_device *obd)
1650 struct list_head *tmp;
1652 __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
1656 if (obd->obd_recovery_data.trd_processing_task == cfs_curproc_pid()) {
1657 /* Processing the queue right now, don't re-add. */
1661 target_process_req_flags(obd, req);
1663 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
1664 /* client declares he's ready to complete recovery
1665 * so, we put the request on th final queue */
1666 req = ptlrpc_clone_req(req);
1669 DEBUG_REQ(D_HA, req, "queue final req");
1670 spin_lock_bh(&obd->obd_processing_task_lock);
1671 if (obd->obd_recovering)
1672 list_add_tail(&req->rq_list, &obd->obd_final_req_queue);
1674 spin_unlock_bh(&obd->obd_processing_task_lock);
1675 ptlrpc_free_clone(req);
1676 if (obd->obd_stopping) {
1682 spin_unlock_bh(&obd->obd_processing_task_lock);
1685 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
1686 /* client declares he's ready to replay locks */
1687 req = ptlrpc_clone_req(req);
1690 DEBUG_REQ(D_HA, req, "queue lock replay req");
1691 spin_lock_bh(&obd->obd_processing_task_lock);
1692 LASSERT(obd->obd_recovering);
1693 /* usually due to recovery abort */
1694 if (!req->rq_export->exp_in_recovery) {
1695 spin_unlock_bh(&obd->obd_processing_task_lock);
1696 ptlrpc_free_clone(req);
1699 LASSERT(req->rq_export->exp_lock_replay_needed);
1700 list_add_tail(&req->rq_list, &obd->obd_lock_replay_queue);
1701 spin_unlock_bh(&obd->obd_processing_task_lock);
1702 wake_up(&obd->obd_next_transno_waitq);
1706 /* CAVEAT EMPTOR: The incoming request message has been swabbed
1707 * (i.e. buflens etc are in my own byte order), but type-dependent
1708 * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1711 CFS_INIT_LIST_HEAD(&req->rq_list);
1712 DEBUG_REQ(D_HA, req, "not queueing");
1716 spin_lock_bh(&obd->obd_processing_task_lock);
1718 /* If we're processing the queue, we want don't want to queue this
1721 * Also, if this request has a transno less than the one we're waiting
1722 * for, we should process it now. It could (and currently always will)
1723 * be an open request for a descriptor that was opened some time ago.
1725 * Also, a resent, replayed request that has already been
1726 * handled will pass through here and be processed immediately.
1728 CWARN("Next recovery transno: "LPU64", current: "LPU64", replaying: %i\n",
1729 obd->obd_next_recovery_transno, transno, obd->obd_req_replaying);
1730 if (transno < obd->obd_next_recovery_transno && obd->obd_req_replaying) {
1731 /* Processing the queue right now, don't re-add. */
1732 LASSERT(list_empty(&req->rq_list));
1733 spin_unlock_bh(&obd->obd_processing_task_lock);
1736 spin_unlock_bh(&obd->obd_processing_task_lock);
1738 /* A resent, replayed request that is still on the queue; just drop it.
1739 The queued request will handle this. */
1740 if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT|MSG_REPLAY)) ==
1741 (MSG_RESENT | MSG_REPLAY)) {
1742 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1746 req = ptlrpc_clone_req(req);
1750 spin_lock_bh(&obd->obd_processing_task_lock);
1751 LASSERT(obd->obd_recovering);
1752 if (!req->rq_export->exp_in_recovery) {
1753 spin_unlock_bh(&obd->obd_processing_task_lock);
1754 ptlrpc_free_clone(req);
1757 LASSERT(req->rq_export->exp_req_replay_needed);
1760 list_for_each(tmp, &obd->obd_req_replay_queue) {
1761 struct ptlrpc_request *reqiter =
1762 list_entry(tmp, struct ptlrpc_request, rq_list);
1764 if (lustre_msg_get_transno(reqiter->rq_reqmsg) > transno) {
1765 list_add_tail(&req->rq_list, &reqiter->rq_list);
1772 list_add_tail(&req->rq_list, &obd->obd_req_replay_queue);
1774 obd->obd_requests_queued_for_recovery++;
1775 wake_up(&obd->obd_next_transno_waitq);
1776 spin_unlock_bh(&obd->obd_processing_task_lock);
1781 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1783 return req->rq_export->exp_obd;
1786 static inline struct ldlm_pool *ldlm_exp2pl(struct obd_export *exp)
1788 LASSERT(exp != NULL);
1789 return &exp->exp_obd->obd_namespace->ns_pool;
1792 int target_pack_pool_reply(struct ptlrpc_request *req)
1794 struct ldlm_pool *pl;
1797 if (!exp_connect_lru_resize(req->rq_export))
1800 pl = ldlm_exp2pl(req->rq_export);
1802 spin_lock(&pl->pl_lock);
1803 lustre_msg_set_slv(req->rq_repmsg, ldlm_pool_get_slv(pl));
1804 lustre_msg_set_limit(req->rq_repmsg, ldlm_pool_get_limit(pl));
1805 spin_unlock(&pl->pl_lock);
1810 int target_send_reply_msg(struct ptlrpc_request *req, int rc, int fail_id)
1812 if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1813 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1814 DEBUG_REQ(D_ERROR, req, "dropping reply");
1819 DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
1820 req->rq_status = rc;
1821 return (ptlrpc_error(req));
1823 DEBUG_REQ(D_NET, req, "sending reply");
1826 target_pack_pool_reply(req);
1827 return (ptlrpc_send_reply(req, 1));
1830 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1833 struct ptlrpc_reply_state *rs;
1834 struct obd_device *obd;
1835 struct obd_export *exp;
1836 struct ptlrpc_service *svc;
1838 if (req->rq_no_reply)
1841 svc = req->rq_rqbd->rqbd_service;
1842 rs = req->rq_reply_state;
1843 if (rs == NULL || !rs->rs_difficult) {
1845 target_send_reply_msg (req, rc, fail_id);
1849 /* must be an export if locks saved */
1850 LASSERT (req->rq_export != NULL);
1851 /* req/reply consistent */
1852 LASSERT (rs->rs_service == svc);
1855 LASSERT (!rs->rs_scheduled);
1856 LASSERT (!rs->rs_scheduled_ever);
1857 LASSERT (!rs->rs_handled);
1858 LASSERT (!rs->rs_on_net);
1859 LASSERT (rs->rs_export == NULL);
1860 LASSERT (list_empty(&rs->rs_obd_list));
1861 LASSERT (list_empty(&rs->rs_exp_list));
1863 exp = class_export_get (req->rq_export);
1866 /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1867 rs->rs_scheduled = 1;
1869 rs->rs_xid = req->rq_xid;
1870 rs->rs_transno = req->rq_transno;
1871 rs->rs_export = exp;
1873 spin_lock(&obd->obd_uncommitted_replies_lock);
1875 if (rs->rs_transno > obd->obd_last_committed) {
1876 /* not committed already */
1877 list_add_tail (&rs->rs_obd_list,
1878 &obd->obd_uncommitted_replies);
1881 spin_unlock (&obd->obd_uncommitted_replies_lock);
1882 spin_lock (&exp->exp_lock);
1884 list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1886 spin_unlock(&exp->exp_lock);
1888 netrc = target_send_reply_msg (req, rc, fail_id);
1890 spin_lock(&svc->srv_lock);
1892 svc->srv_n_difficult_replies++;
1895 /* error sending: reply is off the net. Also we need +1
1896 * reply ref until ptlrpc_server_handle_reply() is done
1897 * with the reply state (if the send was successful, there
1898 * would have been +1 ref for the net, which
1899 * reply_out_callback leaves alone) */
1901 ptlrpc_rs_addref(rs);
1902 atomic_inc (&svc->srv_outstanding_replies);
1905 if (!rs->rs_on_net || /* some notifier */
1906 list_empty(&rs->rs_exp_list) || /* completed already */
1907 list_empty(&rs->rs_obd_list)) {
1908 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1909 cfs_waitq_signal (&svc->srv_waitq);
1911 list_add (&rs->rs_list, &svc->srv_active_replies);
1912 rs->rs_scheduled = 0; /* allow notifier to schedule */
1915 spin_unlock(&svc->srv_lock);
1918 int target_handle_ping(struct ptlrpc_request *req)
1920 obd_ping(req->rq_export);
1921 return lustre_pack_reply(req, 1, NULL, NULL);
1924 void target_committed_to_req(struct ptlrpc_request *req)
1926 struct obd_device *obd;
1928 if (req == NULL || req->rq_export == NULL)
1931 obd = req->rq_export->exp_obd;
1935 if (!obd->obd_no_transno && req->rq_repmsg != NULL)
1936 lustre_msg_set_last_committed(req->rq_repmsg,
1937 obd->obd_last_committed);
1939 DEBUG_REQ(D_IOCTL, req, "not sending last_committed update");
1941 CDEBUG(D_INFO, "last_committed "LPU64", transno "LPU64", xid "LPU64"\n",
1942 obd->obd_last_committed, req->rq_transno, req->rq_xid);
1945 EXPORT_SYMBOL(target_committed_to_req);
1947 #ifdef HAVE_QUOTA_SUPPORT
1948 int target_handle_qc_callback(struct ptlrpc_request *req)
1950 struct obd_quotactl *oqctl;
1951 struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
1953 oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1954 lustre_swab_obd_quotactl);
1955 if (oqctl == NULL) {
1956 CERROR("Can't unpack obd_quotactl\n");
1960 cli->cl_qchk_stat = oqctl->qc_stat;
1965 int target_handle_dqacq_callback(struct ptlrpc_request *req)
1968 struct obd_device *obd = req->rq_export->exp_obd;
1969 struct obd_device *master_obd;
1970 struct lustre_quota_ctxt *qctxt;
1971 struct qunit_data *qdata;
1973 struct qunit_data_old *qdata_old;
1975 int repsize[2] = { sizeof(struct ptlrpc_body),
1976 sizeof(struct qunit_data) };
1979 rc = lustre_pack_reply(req, 2, repsize, NULL);
1981 CERROR("packing reply failed!: rc = %d\n", rc);
1984 LASSERT(req->rq_export);
1986 /* fixed for bug10707 */
1987 if ((req->rq_export->exp_connect_flags & OBD_CONNECT_QUOTA64) &&
1988 !OBD_FAIL_CHECK(OBD_FAIL_QUOTA_QD_COUNT_32BIT)) {
1989 CDEBUG(D_QUOTA, "qd_count is 64bit!\n");
1990 rep = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1991 sizeof(struct qunit_data));
1993 qdata = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*qdata),
1996 CDEBUG(D_QUOTA, "qd_count is 32bit!\n");
1997 rep = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1998 sizeof(struct qunit_data_old));
2000 qdata_old = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*qdata_old),
2001 lustre_swab_qdata_old);
2002 qdata = lustre_quota_old_to_new(qdata_old);
2005 if (qdata == NULL) {
2006 CERROR("Can't unpack qunit_data\n");
2010 /* we use the observer */
2011 LASSERT(obd->obd_observer && obd->obd_observer->obd_observer);
2012 master_obd = obd->obd_observer->obd_observer;
2013 qctxt = &master_obd->u.obt.obt_qctxt;
2015 LASSERT(qctxt->lqc_handler);
2016 rc = qctxt->lqc_handler(master_obd, qdata,
2017 lustre_msg_get_opc(req->rq_reqmsg));
2018 if (rc && rc != -EDQUOT)
2019 CDEBUG(rc == -EBUSY ? D_QUOTA : D_ERROR,
2020 "dqacq failed! (rc:%d)\n", rc);
2022 /* the qd_count might be changed in lqc_handler */
2023 if ((req->rq_export->exp_connect_flags & OBD_CONNECT_QUOTA64) &&
2024 !OBD_FAIL_CHECK(OBD_FAIL_QUOTA_QD_COUNT_32BIT)) {
2025 memcpy(rep,qdata,sizeof(*qdata));
2027 qdata_old = lustre_quota_new_to_old(qdata);
2028 memcpy(rep,qdata_old,sizeof(*qdata_old));
2030 req->rq_status = rc;
2031 rc = ptlrpc_reply(req);
2036 #endif /* !__KERNEL__ */
2038 #endif /* HAVE_QUOTA_SUPPORT */
2040 ldlm_mode_t lck_compat_array[] = {
2041 [LCK_EX] LCK_COMPAT_EX,
2042 [LCK_PW] LCK_COMPAT_PW,
2043 [LCK_PR] LCK_COMPAT_PR,
2044 [LCK_CW] LCK_COMPAT_CW,
2045 [LCK_CR] LCK_COMPAT_CR,
2046 [LCK_NL] LCK_COMPAT_NL,
2047 [LCK_GROUP] LCK_COMPAT_GROUP