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;
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);
354 int client_obd_cleanup(struct obd_device *obddev)
357 ldlm_put_ref(obddev->obd_force);
362 /* ->o_connect() method for client side (OSC and MDC and MGC) */
363 int client_connect_import(const struct lu_env *env,
364 struct lustre_handle *dlm_handle,
365 struct obd_device *obd, struct obd_uuid *cluuid,
366 struct obd_connect_data *data)
368 struct client_obd *cli = &obd->u.cli;
369 struct obd_import *imp = cli->cl_import;
370 struct obd_export *exp;
371 struct obd_connect_data *ocd;
375 mutex_down(&cli->cl_sem);
376 rc = class_connect(dlm_handle, obd, cluuid);
380 cli->cl_conn_count++;
381 if (cli->cl_conn_count > 1)
383 exp = class_conn2export(dlm_handle);
385 if (obd->obd_namespace != NULL)
386 CERROR("already have namespace!\n");
387 obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
388 LDLM_NAMESPACE_CLIENT);
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 if (lustre_msg_get_op_flags(req->rq_repmsg) & MSG_CONNECT_RECONNECT) {
874 LASSERT(export->exp_imp_reverse);
875 sptlrpc_svc_install_rvs_ctx(export->exp_imp_reverse,
880 spin_lock_bh(&target->obd_processing_task_lock);
881 if (target->obd_recovering && !export->exp_in_recovery) {
882 spin_lock(&export->exp_lock);
883 export->exp_in_recovery = 1;
884 export->exp_req_replay_needed = 1;
885 export->exp_lock_replay_needed = 1;
886 spin_unlock(&export->exp_lock);
887 if ((lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_TRANSNO)
888 && data->ocd_transno < target->obd_next_recovery_transno)
889 target->obd_next_recovery_transno = data->ocd_transno;
890 target->obd_connected_clients++;
891 /* each connected client is counted as recoverable */
892 target->obd_recoverable_clients++;
893 atomic_inc(&target->obd_req_replay_clients);
894 atomic_inc(&target->obd_lock_replay_clients);
895 if (target->obd_connected_clients ==
896 target->obd_max_recoverable_clients)
897 wake_up(&target->obd_next_transno_waitq);
899 spin_unlock_bh(&target->obd_processing_task_lock);
901 lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 2, sizeof conn),
904 if (export->exp_imp_reverse != NULL) {
905 /* destroyed import can be still referenced in ctxt */
906 obd_set_info_async(export, strlen(KEY_REVIMP_UPD),
907 KEY_REVIMP_UPD, 0, NULL, NULL);
908 destroy_import(export->exp_imp_reverse);
911 /* for the rest part, we return -ENOTCONN in case of errors
912 * in order to let client initialize connection again.
914 revimp = export->exp_imp_reverse = class_new_import(target);
916 CERROR("fail to alloc new reverse import.\n");
917 GOTO(out, rc = -ENOTCONN);
920 revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
921 revimp->imp_client = &export->exp_obd->obd_ldlm_client;
922 revimp->imp_remote_handle = conn;
923 revimp->imp_dlm_fake = 1;
924 revimp->imp_state = LUSTRE_IMP_FULL;
926 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_NEXT_VER) {
927 revimp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
928 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_NEXT_VER);
931 rc = sptlrpc_import_get_sec(revimp, req->rq_svc_ctx,
932 req->rq_sec_flavor, 0);
934 CERROR("Failed to get sec for reverse import: %d\n", rc);
935 export->exp_imp_reverse = NULL;
936 class_destroy_import(revimp);
939 class_import_put(revimp);
942 spin_lock(&export->exp_lock);
943 export->exp_connecting = 0;
944 spin_unlock(&export->exp_lock);
947 class_decref(targref);
953 int target_handle_disconnect(struct ptlrpc_request *req)
958 rc = lustre_pack_reply(req, 1, NULL, NULL);
962 /* keep the rq_export around so we can send the reply */
963 req->rq_status = obd_disconnect(class_export_get(req->rq_export));
968 void target_destroy_export(struct obd_export *exp)
970 /* exports created from last_rcvd data, and "fake"
971 exports created by lctl don't have an import */
972 if (exp->exp_imp_reverse != NULL)
973 destroy_import(exp->exp_imp_reverse);
975 /* We cancel locks at disconnect time, but this will catch any locks
976 * granted in a race with recovery-induced disconnect. */
977 if (exp->exp_obd->obd_namespace != NULL)
978 ldlm_cancel_locks_for_export(exp);
985 struct ptlrpc_request *ptlrpc_clone_req( struct ptlrpc_request *orig_req)
987 struct ptlrpc_request *copy_req;
988 struct lustre_msg *copy_reqmsg;
989 struct ptlrpc_user_desc *udesc = NULL;
991 OBD_ALLOC_PTR(copy_req);
994 OBD_ALLOC(copy_reqmsg, orig_req->rq_reqlen);
996 OBD_FREE_PTR(copy_req);
1000 if (orig_req->rq_user_desc) {
1001 int ngroups = orig_req->rq_user_desc->pud_ngroups;
1003 OBD_ALLOC(udesc, sptlrpc_user_desc_size(ngroups));
1005 OBD_FREE(copy_reqmsg, orig_req->rq_reqlen);
1006 OBD_FREE_PTR(copy_req);
1009 memcpy(udesc, orig_req->rq_user_desc,
1010 sptlrpc_user_desc_size(ngroups));
1013 *copy_req = *orig_req;
1014 memcpy(copy_reqmsg, orig_req->rq_reqmsg, orig_req->rq_reqlen);
1015 copy_req->rq_reqmsg = copy_reqmsg;
1016 copy_req->rq_user_desc = udesc;
1018 class_export_get(copy_req->rq_export);
1019 CFS_INIT_LIST_HEAD(©_req->rq_list);
1020 sptlrpc_svc_ctx_addref(copy_req);
1022 if (copy_req->rq_reply_state) {
1023 /* the copied req takes over the reply state */
1024 orig_req->rq_reply_state = NULL;
1025 /* to catch further access */
1026 orig_req->rq_repmsg = NULL;
1027 orig_req->rq_replen = 0;
1033 void ptlrpc_free_clone( struct ptlrpc_request *req)
1035 if (req->rq_reply_state) {
1036 ptlrpc_rs_decref(req->rq_reply_state);
1037 req->rq_reply_state = NULL;
1040 sptlrpc_svc_ctx_decref(req);
1041 class_export_put(req->rq_export);
1042 list_del(&req->rq_list);
1044 if (req->rq_user_desc) {
1045 int ngroups = req->rq_user_desc->pud_ngroups;
1046 OBD_FREE(req->rq_user_desc, sptlrpc_user_desc_size(ngroups));
1048 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
1053 static void target_finish_recovery(struct obd_device *obd)
1056 LCONSOLE_INFO("%s: sending delayed replies to recovered clients\n",
1059 ldlm_reprocess_all_ns(obd->obd_namespace);
1061 /* when recovery finished, cleanup orphans on mds and ost */
1062 if (OBT(obd) && OBP(obd, postrecov)) {
1063 int rc = OBP(obd, postrecov)(obd);
1064 LCONSOLE_WARN("%s: recovery %s: rc %d\n", obd->obd_name,
1065 rc < 0 ? "failed" : "complete", rc);
1068 obd->obd_recovery_end = CURRENT_SECONDS;
1072 static void abort_req_replay_queue(struct obd_device *obd)
1074 struct ptlrpc_request *req, *n;
1076 list_for_each_entry_safe(req, n, &obd->obd_req_replay_queue, rq_list) {
1077 DEBUG_REQ(D_WARNING, req, "aborted:");
1078 req->rq_status = -ENOTCONN;
1079 if (ptlrpc_error(req)) {
1080 DEBUG_REQ(D_ERROR, req,
1081 "failed abort_req_reply; skipping");
1083 ptlrpc_free_clone(req);
1087 static void abort_lock_replay_queue(struct obd_device *obd)
1089 struct ptlrpc_request *req, *n;
1091 list_for_each_entry_safe(req, n, &obd->obd_lock_replay_queue, rq_list){
1092 DEBUG_REQ(D_ERROR, req, "aborted:");
1093 req->rq_status = -ENOTCONN;
1094 if (ptlrpc_error(req)) {
1095 DEBUG_REQ(D_ERROR, req,
1096 "failed abort_lock_reply; skipping");
1098 ptlrpc_free_clone(req);
1103 /* Called from a cleanup function if the device is being cleaned up
1104 forcefully. The exports should all have been disconnected already,
1105 the only thing left to do is
1106 - clear the recovery flags
1108 - free queued requests and replies, but don't send replies
1109 Because the obd_stopping flag is set, no new requests should be received.
1112 void target_cleanup_recovery(struct obd_device *obd)
1114 struct ptlrpc_request *req, *n;
1117 LASSERT(obd->obd_stopping);
1119 spin_lock_bh(&obd->obd_processing_task_lock);
1120 if (!obd->obd_recovering) {
1121 spin_unlock_bh(&obd->obd_processing_task_lock);
1125 obd->obd_recovering = obd->obd_abort_recovery = 0;
1126 target_cancel_recovery_timer(obd);
1127 spin_unlock_bh(&obd->obd_processing_task_lock);
1129 list_for_each_entry_safe(req, n, &obd->obd_req_replay_queue, rq_list) {
1130 LASSERT (req->rq_reply_state == 0);
1131 ptlrpc_free_clone(req);
1133 list_for_each_entry_safe(req, n, &obd->obd_lock_replay_queue, rq_list){
1134 LASSERT (req->rq_reply_state == 0);
1135 ptlrpc_free_clone(req);
1137 list_for_each_entry_safe(req, n, &obd->obd_final_req_queue, rq_list) {
1138 LASSERT (req->rq_reply_state == 0);
1139 ptlrpc_free_clone(req);
1145 static void target_recovery_expired(unsigned long castmeharder)
1147 struct obd_device *obd = (struct obd_device *)castmeharder;
1148 CERROR("%s: recovery timed out, aborting\n", obd->obd_name);
1149 spin_lock_bh(&obd->obd_processing_task_lock);
1150 if (obd->obd_recovering)
1151 obd->obd_abort_recovery = 1;
1152 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1153 spin_unlock_bh(&obd->obd_processing_task_lock);
1157 /* obd_processing_task_lock should be held */
1158 void target_cancel_recovery_timer(struct obd_device *obd)
1160 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
1161 cfs_timer_disarm(&obd->obd_recovery_timer);
1164 static void reset_recovery_timer(struct obd_device *obd)
1166 spin_lock_bh(&obd->obd_processing_task_lock);
1167 if (!obd->obd_recovering) {
1168 spin_unlock_bh(&obd->obd_processing_task_lock);
1171 cfs_timer_arm(&obd->obd_recovery_timer,
1172 cfs_time_shift(OBD_RECOVERY_TIMEOUT));
1173 spin_unlock_bh(&obd->obd_processing_task_lock);
1174 CDEBUG(D_HA, "%s: timer will expire in %u seconds\n", obd->obd_name,
1175 OBD_RECOVERY_TIMEOUT);
1176 /* Only used for lprocfs_status */
1177 obd->obd_recovery_end = CURRENT_SECONDS + OBD_RECOVERY_TIMEOUT;
1181 /* Only start it the first time called */
1182 void target_start_recovery_timer(struct obd_device *obd)
1184 spin_lock_bh(&obd->obd_processing_task_lock);
1185 if (obd->obd_recovery_handler
1186 || timer_pending((struct timer_list *)&obd->obd_recovery_timer)) {
1187 spin_unlock_bh(&obd->obd_processing_task_lock);
1190 CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
1191 OBD_RECOVERY_TIMEOUT);
1192 cfs_timer_init(&obd->obd_recovery_timer, target_recovery_expired, obd);
1193 spin_unlock_bh(&obd->obd_processing_task_lock);
1195 reset_recovery_timer(obd);
1199 static int check_for_next_transno(struct obd_device *obd)
1201 struct ptlrpc_request *req = NULL;
1202 int wake_up = 0, connected, completed, queue_len, max;
1203 __u64 next_transno, req_transno;
1205 spin_lock_bh(&obd->obd_processing_task_lock);
1207 if (!list_empty(&obd->obd_req_replay_queue)) {
1208 req = list_entry(obd->obd_req_replay_queue.next,
1209 struct ptlrpc_request, rq_list);
1210 req_transno = lustre_msg_get_transno(req->rq_reqmsg);
1215 max = obd->obd_max_recoverable_clients;
1216 connected = obd->obd_connected_clients;
1217 completed = connected - obd->obd_recoverable_clients;
1218 queue_len = obd->obd_requests_queued_for_recovery;
1219 next_transno = obd->obd_next_recovery_transno;
1221 CDEBUG(D_HA, "max: %d, connected: %d, completed: %d, queue_len: %d, "
1222 "req_transno: "LPU64", next_transno: "LPU64"\n",
1223 max, connected, completed, queue_len, req_transno, next_transno);
1225 if (obd->obd_abort_recovery) {
1226 CDEBUG(D_HA, "waking for aborted recovery\n");
1228 } else if (atomic_read(&obd->obd_req_replay_clients) == 0) {
1229 CDEBUG(D_HA, "waking for completed recovery\n");
1231 } else if (req_transno == next_transno) {
1232 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1234 } else if (queue_len + completed == max) {
1235 /* handle gaps occured due to lost reply. It is allowed gaps
1236 * because all clients are connected and there will be resend
1237 * for missed transaction */
1238 LASSERTF(req_transno >= next_transno,
1239 "req_transno: "LPU64", next_transno: "LPU64"\n",
1240 req_transno, next_transno);
1242 CDEBUG(req_transno > obd->obd_last_committed ? D_ERROR : D_HA,
1243 "waking for skipped transno (skip: "LPD64
1244 ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
1245 next_transno, queue_len, completed, connected, req_transno);
1246 obd->obd_next_recovery_transno = req_transno;
1248 } else if (queue_len == atomic_read(&obd->obd_req_replay_clients)) {
1249 /* some clients haven't connected in time, but we can try
1250 * to replay requests that demand on already committed ones
1251 * also, we can replay first non-committed transation */
1252 LASSERT(req_transno != 0);
1253 if (req_transno == obd->obd_last_committed + 1) {
1254 obd->obd_next_recovery_transno = req_transno;
1255 } else if (req_transno > obd->obd_last_committed) {
1256 /* can't continue recovery: have no needed transno */
1257 obd->obd_abort_recovery = 1;
1258 CDEBUG(D_ERROR, "abort due to missed clients. max: %d, "
1259 "connected: %d, completed: %d, queue_len: %d, "
1260 "req_transno: "LPU64", next_transno: "LPU64"\n",
1261 max, connected, completed, queue_len,
1262 req_transno, next_transno);
1267 spin_unlock_bh(&obd->obd_processing_task_lock);
1271 static struct ptlrpc_request *target_next_replay_req(struct obd_device *obd)
1273 struct l_wait_info lwi = { 0 };
1274 struct ptlrpc_request *req;
1276 CDEBUG(D_HA, "Waiting for transno "LPD64"\n",
1277 obd->obd_next_recovery_transno);
1278 l_wait_event(obd->obd_next_transno_waitq,
1279 check_for_next_transno(obd), &lwi);
1281 spin_lock_bh(&obd->obd_processing_task_lock);
1282 if (obd->obd_abort_recovery) {
1284 } else if (!list_empty(&obd->obd_req_replay_queue)) {
1285 req = list_entry(obd->obd_req_replay_queue.next,
1286 struct ptlrpc_request, rq_list);
1287 list_del_init(&req->rq_list);
1288 obd->obd_requests_queued_for_recovery--;
1292 spin_unlock_bh(&obd->obd_processing_task_lock);
1296 static int check_for_next_lock(struct obd_device *obd)
1298 struct ptlrpc_request *req = NULL;
1301 spin_lock_bh(&obd->obd_processing_task_lock);
1302 if (!list_empty(&obd->obd_lock_replay_queue)) {
1303 req = list_entry(obd->obd_lock_replay_queue.next,
1304 struct ptlrpc_request, rq_list);
1305 CDEBUG(D_HA, "waking for next lock\n");
1307 } else if (atomic_read(&obd->obd_lock_replay_clients) == 0) {
1308 CDEBUG(D_HA, "waking for completed lock replay\n");
1310 } else if (obd->obd_abort_recovery) {
1311 CDEBUG(D_HA, "waking for aborted recovery\n");
1314 spin_unlock_bh(&obd->obd_processing_task_lock);
1319 static struct ptlrpc_request *target_next_replay_lock(struct obd_device *obd)
1321 struct l_wait_info lwi = { 0 };
1322 struct ptlrpc_request *req;
1324 CDEBUG(D_HA, "Waiting for lock\n");
1325 l_wait_event(obd->obd_next_transno_waitq,
1326 check_for_next_lock(obd), &lwi);
1328 spin_lock_bh(&obd->obd_processing_task_lock);
1329 if (obd->obd_abort_recovery) {
1331 } else if (!list_empty(&obd->obd_lock_replay_queue)) {
1332 req = list_entry(obd->obd_lock_replay_queue.next,
1333 struct ptlrpc_request, rq_list);
1334 list_del_init(&req->rq_list);
1338 spin_unlock_bh(&obd->obd_processing_task_lock);
1342 static struct ptlrpc_request *target_next_final_ping(struct obd_device *obd)
1344 struct ptlrpc_request *req;
1346 spin_lock_bh(&obd->obd_processing_task_lock);
1347 if (!list_empty(&obd->obd_final_req_queue)) {
1348 req = list_entry(obd->obd_final_req_queue.next,
1349 struct ptlrpc_request, rq_list);
1350 list_del_init(&req->rq_list);
1354 spin_unlock_bh(&obd->obd_processing_task_lock);
1358 static inline int req_replay_done(struct obd_export *exp)
1360 return (exp->exp_req_replay_needed == 0);
1363 static inline int lock_replay_done(struct obd_export *exp)
1365 return (exp->exp_lock_replay_needed == 0);
1368 static inline int connect_done(struct obd_export *exp)
1370 return (exp->exp_in_recovery != 0);
1373 static int check_for_clients(struct obd_device *obd)
1375 if (obd->obd_abort_recovery)
1377 LASSERT(obd->obd_connected_clients <= obd->obd_max_recoverable_clients);
1378 if (obd->obd_no_conn == 0 &&
1379 obd->obd_connected_clients == obd->obd_max_recoverable_clients)
1384 static int handle_recovery_req(struct ptlrpc_thread *thread,
1385 struct ptlrpc_request *req,
1386 svc_handler_t handler)
1391 rc = lu_context_init(&req->rq_session, LCT_SESSION);
1393 CERROR("Failure to initialize session: %d\n", rc);
1396 req->rq_session.lc_thread = thread;
1397 lu_context_enter(&req->rq_session);
1398 req->rq_svc_thread = thread;
1399 req->rq_svc_thread->t_env->le_ses = &req->rq_session;
1403 lu_context_exit(&req->rq_session);
1404 lu_context_fini(&req->rq_session);
1405 /* don't reset timer for final stage */
1406 if (!req_replay_done(req->rq_export) ||
1407 !lock_replay_done(req->rq_export))
1408 reset_recovery_timer(class_exp2obd(req->rq_export));
1409 ptlrpc_free_clone(req);
1413 static int target_recovery_thread(void *arg)
1415 struct obd_device *obd = arg;
1416 struct ptlrpc_request *req;
1417 struct target_recovery_data *trd = &obd->obd_recovery_data;
1418 struct l_wait_info lwi = { 0 };
1419 unsigned long delta;
1420 unsigned long flags;
1422 struct ptlrpc_thread fake_svc_thread, *thread = &fake_svc_thread;
1423 __u32 recov_ctx_tags = LCT_MD_THREAD;
1427 cfs_daemonize("tgt_recov");
1429 SIGNAL_MASK_LOCK(current, flags);
1430 sigfillset(¤t->blocked);
1432 SIGNAL_MASK_UNLOCK(current, flags);
1434 rc = lu_context_init(&env.le_ctx, recov_ctx_tags);
1438 thread->t_env = &env;
1439 env.le_ctx.lc_thread = thread;
1441 CERROR("%s: started recovery thread pid %d\n", obd->obd_name,
1443 trd->trd_processing_task = current->pid;
1445 obd->obd_recovering = 1;
1446 complete(&trd->trd_starting);
1448 /* first of all, we have to know the first transno to replay */
1449 obd->obd_abort_recovery = 0;
1450 l_wait_event(obd->obd_next_transno_waitq,
1451 check_for_clients(obd), &lwi);
1453 spin_lock_bh(&obd->obd_processing_task_lock);
1454 target_cancel_recovery_timer(obd);
1455 spin_unlock_bh(&obd->obd_processing_task_lock);
1457 /* If some clients haven't connected in time, evict them */
1458 if (obd->obd_abort_recovery) {
1459 CWARN("Some clients haven't connect in time (%d/%d),"
1460 "evict them\n", obd->obd_connected_clients,
1461 obd->obd_max_recoverable_clients);
1462 obd->obd_abort_recovery = obd->obd_stopping;
1463 class_disconnect_stale_exports(obd, connect_done);
1465 /* next stage: replay requests */
1467 obd->obd_req_replaying = 1;
1468 CDEBUG(D_INFO, "1: request replay stage - %d clients from t"LPU64"\n",
1469 atomic_read(&obd->obd_req_replay_clients),
1470 obd->obd_next_recovery_transno);
1471 while ((req = target_next_replay_req(obd))) {
1472 LASSERT(trd->trd_processing_task == current->pid);
1473 DEBUG_REQ(D_HA, req, "processing t"LPD64" from %s",
1474 lustre_msg_get_transno(req->rq_reqmsg),
1475 libcfs_nid2str(req->rq_peer.nid));
1477 handle_recovery_req(thread, req,
1478 trd->trd_recovery_handler);
1479 obd->obd_replayed_requests++;
1480 spin_lock_bh(&obd->obd_processing_task_lock);
1481 obd->obd_next_recovery_transno++;
1482 spin_unlock_bh(&obd->obd_processing_task_lock);
1485 spin_lock_bh(&obd->obd_processing_task_lock);
1486 target_cancel_recovery_timer(obd);
1487 spin_unlock_bh(&obd->obd_processing_task_lock);
1489 /* If some clients haven't replayed requests in time, evict them */
1490 if (obd->obd_abort_recovery) {
1491 CDEBUG(D_ERROR, "req replay timed out, aborting ...\n");
1492 obd->obd_abort_recovery = obd->obd_stopping;
1493 class_disconnect_stale_exports(obd, req_replay_done);
1494 abort_req_replay_queue(obd);
1496 /* The second stage: replay locks */
1497 CDEBUG(D_INFO, "2: lock replay stage - %d clients\n",
1498 atomic_read(&obd->obd_lock_replay_clients));
1499 while ((req = target_next_replay_lock(obd))) {
1500 LASSERT(trd->trd_processing_task == current->pid);
1501 DEBUG_REQ(D_HA|D_WARNING, req, "processing lock from %s: ",
1502 libcfs_nid2str(req->rq_peer.nid));
1503 handle_recovery_req(thread, req,
1504 trd->trd_recovery_handler);
1505 obd->obd_replayed_locks++;
1508 spin_lock_bh(&obd->obd_processing_task_lock);
1509 target_cancel_recovery_timer(obd);
1510 spin_unlock_bh(&obd->obd_processing_task_lock);
1511 /* If some clients haven't replayed requests in time, evict them */
1512 if (obd->obd_abort_recovery) {
1514 CERROR("lock replay timed out, aborting ...\n");
1515 obd->obd_abort_recovery = obd->obd_stopping;
1516 stale = class_disconnect_stale_exports(obd, lock_replay_done);
1517 abort_lock_replay_queue(obd);
1520 /* We drop recoverying flag to forward all new requests
1521 * to regular mds_handle() since now */
1522 spin_lock_bh(&obd->obd_processing_task_lock);
1523 obd->obd_recovering = obd->obd_abort_recovery = 0;
1524 spin_unlock_bh(&obd->obd_processing_task_lock);
1525 /* The third stage: reply on final pings */
1526 CDEBUG(D_INFO, "3: final stage - process recovery completion pings\n");
1527 while ((req = target_next_final_ping(obd))) {
1528 LASSERT(trd->trd_processing_task == current->pid);
1529 DEBUG_REQ(D_HA, req, "processing final ping from %s: ",
1530 libcfs_nid2str(req->rq_peer.nid));
1531 handle_recovery_req(thread, req,
1532 trd->trd_recovery_handler);
1535 delta = (jiffies - delta) / HZ;
1536 CDEBUG(D_INFO,"4: recovery completed in %lus - %d/%d reqs/locks\n",
1537 delta, obd->obd_replayed_requests, obd->obd_replayed_locks);
1538 LASSERT(atomic_read(&obd->obd_req_replay_clients) == 0);
1539 LASSERT(atomic_read(&obd->obd_lock_replay_clients) == 0);
1540 if (delta > obd_timeout * 2) {
1541 CWARN("too long recovery - read logs\n");
1542 libcfs_debug_dumplog();
1545 target_finish_recovery(obd);
1548 trd->trd_processing_task = 0;
1549 complete(&trd->trd_finishing);
1553 int target_start_recovery_thread(struct obd_device *obd, svc_handler_t handler)
1556 struct target_recovery_data *trd = &obd->obd_recovery_data;
1558 memset(trd, 0, sizeof(*trd));
1559 init_completion(&trd->trd_starting);
1560 init_completion(&trd->trd_finishing);
1561 trd->trd_recovery_handler = handler;
1563 if (kernel_thread(target_recovery_thread, obd, 0) > 0) {
1564 wait_for_completion(&trd->trd_starting);
1565 LASSERT(obd->obd_recovering != 0);
1572 void target_stop_recovery_thread(struct obd_device *obd)
1574 spin_lock_bh(&obd->obd_processing_task_lock);
1575 if (obd->obd_recovery_data.trd_processing_task > 0) {
1576 struct target_recovery_data *trd = &obd->obd_recovery_data;
1577 CERROR("%s: Aborting recovery\n", obd->obd_name);
1578 obd->obd_abort_recovery = 1;
1579 wake_up(&obd->obd_next_transno_waitq);
1580 spin_unlock_bh(&obd->obd_processing_task_lock);
1581 wait_for_completion(&trd->trd_finishing);
1583 spin_unlock_bh(&obd->obd_processing_task_lock);
1587 void target_recovery_fini(struct obd_device *obd)
1589 class_disconnect_exports(obd);
1590 target_stop_recovery_thread(obd);
1591 target_cleanup_recovery(obd);
1593 EXPORT_SYMBOL(target_recovery_fini);
1595 void target_recovery_init(struct obd_device *obd, svc_handler_t handler)
1597 if (obd->obd_max_recoverable_clients == 0)
1600 CWARN("RECOVERY: service %s, %d recoverable clients, "
1601 "last_transno "LPU64"\n", obd->obd_name,
1602 obd->obd_max_recoverable_clients, obd->obd_last_committed);
1603 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
1604 target_start_recovery_thread(obd, handler);
1605 obd->obd_recovery_start = CURRENT_SECONDS;
1606 /* Only used for lprocfs_status */
1607 obd->obd_recovery_end = obd->obd_recovery_start + OBD_RECOVERY_TIMEOUT;
1609 EXPORT_SYMBOL(target_recovery_init);
1613 int target_process_req_flags(struct obd_device *obd, struct ptlrpc_request *req)
1615 struct obd_export *exp = req->rq_export;
1616 LASSERT(exp != NULL);
1617 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
1618 /* client declares he's ready to replay locks */
1619 spin_lock_bh(&obd->obd_processing_task_lock);
1620 if (exp->exp_req_replay_needed) {
1621 LASSERT(atomic_read(&obd->obd_req_replay_clients) > 0);
1622 spin_lock(&exp->exp_lock);
1623 exp->exp_req_replay_needed = 0;
1624 spin_unlock(&exp->exp_lock);
1625 atomic_dec(&obd->obd_req_replay_clients);
1626 LASSERT(obd->obd_recoverable_clients > 0);
1627 obd->obd_recoverable_clients--;
1628 if (atomic_read(&obd->obd_req_replay_clients) == 0)
1629 CDEBUG(D_HA, "all clients have replayed reqs\n");
1630 wake_up(&obd->obd_next_transno_waitq);
1632 spin_unlock_bh(&obd->obd_processing_task_lock);
1634 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
1635 /* client declares he's ready to complete recovery
1636 * so, we put the request on th final queue */
1637 spin_lock_bh(&obd->obd_processing_task_lock);
1638 if (exp->exp_lock_replay_needed) {
1639 LASSERT(atomic_read(&obd->obd_lock_replay_clients) > 0);
1640 spin_lock(&exp->exp_lock);
1641 exp->exp_lock_replay_needed = 0;
1642 spin_unlock(&exp->exp_lock);
1643 atomic_dec(&obd->obd_lock_replay_clients);
1644 if (atomic_read(&obd->obd_lock_replay_clients) == 0)
1645 CDEBUG(D_HA, "all clients have replayed locks\n");
1646 wake_up(&obd->obd_next_transno_waitq);
1648 spin_unlock_bh(&obd->obd_processing_task_lock);
1654 int target_queue_recovery_request(struct ptlrpc_request *req,
1655 struct obd_device *obd)
1657 struct list_head *tmp;
1659 __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
1663 if (obd->obd_recovery_data.trd_processing_task == cfs_curproc_pid()) {
1664 /* Processing the queue right now, don't re-add. */
1668 target_process_req_flags(obd, req);
1670 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
1671 /* client declares he's ready to complete recovery
1672 * so, we put the request on th final queue */
1673 req = ptlrpc_clone_req(req);
1676 DEBUG_REQ(D_HA, req, "queue final req");
1677 spin_lock_bh(&obd->obd_processing_task_lock);
1678 if (obd->obd_recovering)
1679 list_add_tail(&req->rq_list, &obd->obd_final_req_queue);
1681 spin_unlock_bh(&obd->obd_processing_task_lock);
1682 ptlrpc_free_clone(req);
1683 if (obd->obd_stopping) {
1689 spin_unlock_bh(&obd->obd_processing_task_lock);
1692 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
1693 /* client declares he's ready to replay locks */
1694 req = ptlrpc_clone_req(req);
1697 DEBUG_REQ(D_HA, req, "queue lock replay req");
1698 spin_lock_bh(&obd->obd_processing_task_lock);
1699 LASSERT(obd->obd_recovering);
1700 /* usually due to recovery abort */
1701 if (!req->rq_export->exp_in_recovery) {
1702 spin_unlock_bh(&obd->obd_processing_task_lock);
1703 ptlrpc_free_clone(req);
1706 LASSERT(req->rq_export->exp_lock_replay_needed);
1707 list_add_tail(&req->rq_list, &obd->obd_lock_replay_queue);
1708 spin_unlock_bh(&obd->obd_processing_task_lock);
1709 wake_up(&obd->obd_next_transno_waitq);
1713 /* CAVEAT EMPTOR: The incoming request message has been swabbed
1714 * (i.e. buflens etc are in my own byte order), but type-dependent
1715 * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1718 CFS_INIT_LIST_HEAD(&req->rq_list);
1719 DEBUG_REQ(D_HA, req, "not queueing");
1723 spin_lock_bh(&obd->obd_processing_task_lock);
1725 /* If we're processing the queue, we want don't want to queue this
1728 * Also, if this request has a transno less than the one we're waiting
1729 * for, we should process it now. It could (and currently always will)
1730 * be an open request for a descriptor that was opened some time ago.
1732 * Also, a resent, replayed request that has already been
1733 * handled will pass through here and be processed immediately.
1735 CWARN("Next recovery transno: "LPU64", current: "LPU64", replaying: %i\n",
1736 obd->obd_next_recovery_transno, transno, obd->obd_req_replaying);
1737 if (transno < obd->obd_next_recovery_transno && obd->obd_req_replaying) {
1738 /* Processing the queue right now, don't re-add. */
1739 LASSERT(list_empty(&req->rq_list));
1740 spin_unlock_bh(&obd->obd_processing_task_lock);
1743 spin_unlock_bh(&obd->obd_processing_task_lock);
1745 /* A resent, replayed request that is still on the queue; just drop it.
1746 The queued request will handle this. */
1747 if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT|MSG_REPLAY)) ==
1748 (MSG_RESENT | MSG_REPLAY)) {
1749 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1753 req = ptlrpc_clone_req(req);
1757 spin_lock_bh(&obd->obd_processing_task_lock);
1758 LASSERT(obd->obd_recovering);
1759 if (!req->rq_export->exp_in_recovery) {
1760 spin_unlock_bh(&obd->obd_processing_task_lock);
1761 ptlrpc_free_clone(req);
1764 LASSERT(req->rq_export->exp_req_replay_needed);
1767 list_for_each(tmp, &obd->obd_req_replay_queue) {
1768 struct ptlrpc_request *reqiter =
1769 list_entry(tmp, struct ptlrpc_request, rq_list);
1771 if (lustre_msg_get_transno(reqiter->rq_reqmsg) > transno) {
1772 list_add_tail(&req->rq_list, &reqiter->rq_list);
1779 list_add_tail(&req->rq_list, &obd->obd_req_replay_queue);
1781 obd->obd_requests_queued_for_recovery++;
1782 wake_up(&obd->obd_next_transno_waitq);
1783 spin_unlock_bh(&obd->obd_processing_task_lock);
1788 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1790 return req->rq_export->exp_obd;
1793 int target_send_reply_msg(struct ptlrpc_request *req, int rc, int fail_id)
1795 if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1796 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1797 DEBUG_REQ(D_ERROR, req, "dropping reply");
1802 DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
1803 req->rq_status = rc;
1804 return (ptlrpc_error(req));
1806 DEBUG_REQ(D_NET, req, "sending reply");
1809 return (ptlrpc_send_reply(req, 1));
1812 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1815 struct ptlrpc_reply_state *rs;
1816 struct obd_device *obd;
1817 struct obd_export *exp;
1818 struct ptlrpc_service *svc;
1820 svc = req->rq_rqbd->rqbd_service;
1821 rs = req->rq_reply_state;
1822 if (rs == NULL || !rs->rs_difficult) {
1824 target_send_reply_msg (req, rc, fail_id);
1828 /* must be an export if locks saved */
1829 LASSERT (req->rq_export != NULL);
1830 /* req/reply consistent */
1831 LASSERT (rs->rs_service == svc);
1834 LASSERT (!rs->rs_scheduled);
1835 LASSERT (!rs->rs_scheduled_ever);
1836 LASSERT (!rs->rs_handled);
1837 LASSERT (!rs->rs_on_net);
1838 LASSERT (rs->rs_export == NULL);
1839 LASSERT (list_empty(&rs->rs_obd_list));
1840 LASSERT (list_empty(&rs->rs_exp_list));
1842 exp = class_export_get (req->rq_export);
1845 /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1846 rs->rs_scheduled = 1;
1848 rs->rs_xid = req->rq_xid;
1849 rs->rs_transno = req->rq_transno;
1850 rs->rs_export = exp;
1852 spin_lock(&obd->obd_uncommitted_replies_lock);
1854 if (rs->rs_transno > obd->obd_last_committed) {
1855 /* not committed already */
1856 list_add_tail (&rs->rs_obd_list,
1857 &obd->obd_uncommitted_replies);
1860 spin_unlock (&obd->obd_uncommitted_replies_lock);
1861 spin_lock (&exp->exp_lock);
1863 list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1865 spin_unlock(&exp->exp_lock);
1867 netrc = target_send_reply_msg (req, rc, fail_id);
1869 spin_lock(&svc->srv_lock);
1871 svc->srv_n_difficult_replies++;
1874 /* error sending: reply is off the net. Also we need +1
1875 * reply ref until ptlrpc_server_handle_reply() is done
1876 * with the reply state (if the send was successful, there
1877 * would have been +1 ref for the net, which
1878 * reply_out_callback leaves alone) */
1880 ptlrpc_rs_addref(rs);
1881 atomic_inc (&svc->srv_outstanding_replies);
1884 if (!rs->rs_on_net || /* some notifier */
1885 list_empty(&rs->rs_exp_list) || /* completed already */
1886 list_empty(&rs->rs_obd_list)) {
1887 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1888 cfs_waitq_signal (&svc->srv_waitq);
1890 list_add (&rs->rs_list, &svc->srv_active_replies);
1891 rs->rs_scheduled = 0; /* allow notifier to schedule */
1894 spin_unlock(&svc->srv_lock);
1897 int target_handle_ping(struct ptlrpc_request *req)
1899 obd_ping(req->rq_export);
1900 return lustre_pack_reply(req, 1, NULL, NULL);
1903 void target_committed_to_req(struct ptlrpc_request *req)
1905 struct obd_device *obd;
1907 if (req == NULL || req->rq_export == NULL)
1910 obd = req->rq_export->exp_obd;
1914 if (!obd->obd_no_transno && req->rq_repmsg != NULL)
1915 lustre_msg_set_last_committed(req->rq_repmsg,
1916 obd->obd_last_committed);
1918 DEBUG_REQ(D_IOCTL, req, "not sending last_committed update");
1920 CDEBUG(D_INFO, "last_committed "LPU64", transno "LPU64", xid "LPU64"\n",
1921 obd->obd_last_committed, req->rq_transno, req->rq_xid);
1924 EXPORT_SYMBOL(target_committed_to_req);
1926 #ifdef HAVE_QUOTA_SUPPORT
1927 int target_handle_qc_callback(struct ptlrpc_request *req)
1929 struct obd_quotactl *oqctl;
1930 struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
1932 oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1933 lustre_swab_obd_quotactl);
1934 if (oqctl == NULL) {
1935 CERROR("Can't unpack obd_quotactl\n");
1939 cli->cl_qchk_stat = oqctl->qc_stat;
1944 int target_handle_dqacq_callback(struct ptlrpc_request *req)
1947 struct obd_device *obd = req->rq_export->exp_obd;
1948 struct obd_device *master_obd;
1949 struct lustre_quota_ctxt *qctxt;
1950 struct qunit_data *qdata;
1952 struct qunit_data_old *qdata_old;
1954 int repsize[2] = { sizeof(struct ptlrpc_body),
1955 sizeof(struct qunit_data) };
1958 rc = lustre_pack_reply(req, 2, repsize, NULL);
1960 CERROR("packing reply failed!: rc = %d\n", rc);
1963 LASSERT(req->rq_export);
1965 /* fixed for bug10707 */
1966 if ((req->rq_export->exp_connect_flags & OBD_CONNECT_QUOTA64) &&
1967 !OBD_FAIL_CHECK(OBD_FAIL_QUOTA_QD_COUNT_32BIT)) {
1968 CDEBUG(D_QUOTA, "qd_count is 64bit!\n");
1969 rep = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1970 sizeof(struct qunit_data));
1972 qdata = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*qdata),
1975 CDEBUG(D_QUOTA, "qd_count is 32bit!\n");
1976 rep = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1977 sizeof(struct qunit_data_old));
1979 qdata_old = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*qdata_old),
1980 lustre_swab_qdata_old);
1981 qdata = lustre_quota_old_to_new(qdata_old);
1984 if (qdata == NULL) {
1985 CERROR("Can't unpack qunit_data\n");
1989 /* we use the observer */
1990 LASSERT(obd->obd_observer && obd->obd_observer->obd_observer);
1991 master_obd = obd->obd_observer->obd_observer;
1992 qctxt = &master_obd->u.obt.obt_qctxt;
1994 LASSERT(qctxt->lqc_handler);
1995 rc = qctxt->lqc_handler(master_obd, qdata,
1996 lustre_msg_get_opc(req->rq_reqmsg));
1997 if (rc && rc != -EDQUOT)
1998 CDEBUG(rc == -EBUSY ? D_QUOTA : D_ERROR,
1999 "dqacq failed! (rc:%d)\n", rc);
2001 /* the qd_count might be changed in lqc_handler */
2002 if ((req->rq_export->exp_connect_flags & OBD_CONNECT_QUOTA64) &&
2003 !OBD_FAIL_CHECK(OBD_FAIL_QUOTA_QD_COUNT_32BIT)) {
2004 memcpy(rep,qdata,sizeof(*qdata));
2006 qdata_old = lustre_quota_new_to_old(qdata);
2007 memcpy(rep,qdata_old,sizeof(*qdata_old));
2009 req->rq_status = rc;
2010 rc = ptlrpc_reply(req);
2015 #endif /* !__KERNEL__ */
2017 #endif /* HAVE_QUOTA_SUPPORT */
2019 ldlm_mode_t lck_compat_array[] = {
2020 [LCK_EX] LCK_COMPAT_EX,
2021 [LCK_PW] LCK_COMPAT_PW,
2022 [LCK_PR] LCK_COMPAT_PR,
2023 [LCK_CW] LCK_COMPAT_CW,
2024 [LCK_CR] LCK_COMPAT_CR,
2025 [LCK_NL] LCK_COMPAT_NL,
2026 [LCK_GROUP] LCK_COMPAT_GROUP