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 "ldlm_internal.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);
103 spin_unlock(&imp->imp_lock);
107 OBD_FREE(imp_conn, sizeof(*imp_conn));
109 ptlrpc_put_connection(ptlrpc_conn);
113 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid)
115 return import_set_conn(imp, uuid, 1, 0);
118 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
121 return import_set_conn(imp, uuid, priority, 1);
124 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
126 struct obd_import_conn *imp_conn;
127 struct obd_export *dlmexp;
131 spin_lock(&imp->imp_lock);
132 if (list_empty(&imp->imp_conn_list)) {
133 LASSERT(!imp->imp_connection);
137 list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
138 if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
140 LASSERT(imp_conn->oic_conn);
142 /* is current conn? */
143 if (imp_conn == imp->imp_conn_current) {
144 LASSERT(imp_conn->oic_conn == imp->imp_connection);
146 if (imp->imp_state != LUSTRE_IMP_CLOSED &&
147 imp->imp_state != LUSTRE_IMP_DISCON) {
148 CERROR("can't remove current connection\n");
149 GOTO(out, rc = -EBUSY);
152 ptlrpc_put_connection(imp->imp_connection);
153 imp->imp_connection = NULL;
155 dlmexp = class_conn2export(&imp->imp_dlm_handle);
156 if (dlmexp && dlmexp->exp_connection) {
157 LASSERT(dlmexp->exp_connection ==
159 ptlrpc_put_connection(dlmexp->exp_connection);
160 dlmexp->exp_connection = NULL;
164 list_del(&imp_conn->oic_item);
165 ptlrpc_put_connection(imp_conn->oic_conn);
166 OBD_FREE(imp_conn, sizeof(*imp_conn));
167 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
168 imp, imp->imp_obd->obd_name, uuid->uuid);
173 spin_unlock(&imp->imp_lock);
175 CERROR("connection %s not found\n", uuid->uuid);
179 /* configure an RPC client OBD device
184 * 3 - inactive-on-startup
186 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
188 struct lustre_cfg* lcfg = buf;
189 struct client_obd *cli = &obddev->u.cli;
190 struct obd_import *imp;
191 struct obd_uuid server_uuid;
192 int rq_portal, rp_portal, connect_op;
193 char *name = obddev->obd_type->typ_name;
197 /* In a more perfect world, we would hang a ptlrpc_client off of
198 * obd_type and just use the values from there. */
199 if (!strcmp(name, LUSTRE_OSC_NAME)) {
201 /* Can be removed in Lustre 1.8, for compatibility only */
202 rq_portal = OST_IO_PORTAL;
204 rq_portal = OST_REQUEST_PORTAL;
206 rp_portal = OSC_REPLY_PORTAL;
207 connect_op = OST_CONNECT;
208 } else if (!strcmp(name, LUSTRE_MDC_NAME)) {
209 rq_portal = MDS_REQUEST_PORTAL;
210 rp_portal = MDC_REPLY_PORTAL;
211 connect_op = MDS_CONNECT;
212 } else if (!strcmp(name, LUSTRE_MGC_NAME)) {
213 rq_portal = MGS_REQUEST_PORTAL;
214 rp_portal = MGC_REPLY_PORTAL;
215 connect_op = MGS_CONNECT;
217 CERROR("unknown client OBD type \"%s\", can't setup\n",
222 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
223 CERROR("requires a TARGET UUID\n");
227 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
228 CERROR("client UUID must be less than 38 characters\n");
232 if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
233 CERROR("setup requires a SERVER UUID\n");
237 if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
238 CERROR("target UUID must be less than 38 characters\n");
242 init_rwsem(&cli->cl_sem);
243 sema_init(&cli->cl_mgc_sem, 1);
244 cli->cl_conn_count = 0;
245 memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
246 min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
247 sizeof(server_uuid)));
250 cli->cl_avail_grant = 0;
251 /* FIXME: should limit this for the sum of all cl_dirty_max */
252 cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
253 if (cli->cl_dirty_max >> CFS_PAGE_SHIFT > num_physpages / 8)
254 cli->cl_dirty_max = num_physpages << (CFS_PAGE_SHIFT - 3);
255 CFS_INIT_LIST_HEAD(&cli->cl_cache_waiters);
256 CFS_INIT_LIST_HEAD(&cli->cl_loi_ready_list);
257 CFS_INIT_LIST_HEAD(&cli->cl_loi_write_list);
258 CFS_INIT_LIST_HEAD(&cli->cl_loi_read_list);
259 client_obd_list_lock_init(&cli->cl_loi_list_lock);
260 cli->cl_r_in_flight = 0;
261 cli->cl_w_in_flight = 0;
262 spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
263 spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
264 spin_lock_init(&cli->cl_read_page_hist.oh_lock);
265 spin_lock_init(&cli->cl_write_page_hist.oh_lock);
266 spin_lock_init(&cli->cl_read_offset_hist.oh_lock);
267 spin_lock_init(&cli->cl_write_offset_hist.oh_lock);
268 cfs_waitq_init(&cli->cl_destroy_waitq);
269 atomic_set(&cli->cl_destroy_in_flight, 0);
270 #ifdef ENABLE_CHECKSUM
271 /* Turn on checksumming by default. */
272 cli->cl_checksum = 1;
274 * The supported checksum types will be worked out at connect time
275 * Set cl_chksum* to CRC32 for now to avoid returning screwed info
278 cli->cl_cksum_type = cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
280 atomic_set(&cli->cl_resends, OSC_DEFAULT_RESENDS);
282 /* This value may be changed at connect time in
283 ptlrpc_connect_interpret. */
284 cli->cl_max_pages_per_rpc = min((int)PTLRPC_MAX_BRW_PAGES,
285 (int)(1024 * 1024 >> CFS_PAGE_SHIFT));
287 if (!strcmp(name, LUSTRE_MDC_NAME)) {
288 cli->cl_max_rpcs_in_flight = MDC_MAX_RIF_DEFAULT;
289 } else if (num_physpages >> (20 - CFS_PAGE_SHIFT) <= 128 /* MB */) {
290 cli->cl_max_rpcs_in_flight = 2;
291 } else if (num_physpages >> (20 - CFS_PAGE_SHIFT) <= 256 /* MB */) {
292 cli->cl_max_rpcs_in_flight = 3;
293 } else if (num_physpages >> (20 - CFS_PAGE_SHIFT) <= 512 /* MB */) {
294 cli->cl_max_rpcs_in_flight = 4;
296 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)
361 /* ->o_connect() method for client side (OSC and MDC and MGC) */
362 int client_connect_import(struct lustre_handle *dlm_handle,
363 struct obd_device *obd, struct obd_uuid *cluuid,
364 struct obd_connect_data *data, void *localdata)
366 struct client_obd *cli = &obd->u.cli;
367 struct obd_import *imp = cli->cl_import;
368 struct obd_export *exp;
369 struct obd_connect_data *ocd;
370 struct ldlm_namespace *to_be_freed = NULL;
374 down_write(&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->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 ocd = &imp->imp_connect_data;
400 imp->imp_connect_flags_orig = data->ocd_connect_flags;
403 rc = ptlrpc_connect_import(imp, NULL);
405 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
408 LASSERT(exp->exp_connection);
411 LASSERT((ocd->ocd_connect_flags & data->ocd_connect_flags) ==
412 ocd->ocd_connect_flags);
413 data->ocd_connect_flags = ocd->ocd_connect_flags;
416 ptlrpc_pinger_add_import(imp);
421 ldlm_namespace_free_prior(obd->obd_namespace, imp, 0);
422 to_be_freed = obd->obd_namespace;
423 obd->obd_namespace = NULL;
425 cli->cl_conn_count--;
426 class_disconnect(exp);
428 class_export_put(exp);
431 up_write(&cli->cl_sem);
433 ldlm_namespace_free_post(to_be_freed);
437 int client_disconnect_export(struct obd_export *exp)
439 struct obd_device *obd = class_exp2obd(exp);
440 struct client_obd *cli;
441 struct obd_import *imp;
442 struct ldlm_namespace *to_be_freed = NULL;
447 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
448 exp, exp ? exp->exp_handle.h_cookie : -1);
453 imp = cli->cl_import;
455 down_write(&cli->cl_sem);
456 if (!cli->cl_conn_count) {
457 CERROR("disconnecting disconnected device (%s)\n",
459 GOTO(out_sem, rc = -EINVAL);
462 cli->cl_conn_count--;
463 if (cli->cl_conn_count)
464 GOTO(out_no_disconnect, rc = 0);
466 /* Mark import deactivated now, so we don't try to reconnect if any
467 * of the cleanup RPCs fails (e.g. ldlm cancel, etc). We don't
468 * fully deactivate the import, or that would drop all requests. */
469 spin_lock(&imp->imp_lock);
470 imp->imp_deactive = 1;
471 spin_unlock(&imp->imp_lock);
473 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
474 * delete it regardless. (It's safe to delete an import that was
476 (void)ptlrpc_pinger_del_import(imp);
478 if (obd->obd_namespace != NULL) {
479 /* obd_force == local only */
480 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
481 obd->obd_force ? LDLM_FL_LOCAL_ONLY:0,
483 ldlm_namespace_free_prior(obd->obd_namespace, imp,
485 to_be_freed = obd->obd_namespace;
488 rc = ptlrpc_disconnect_import(imp, 0);
490 ptlrpc_invalidate_import(imp);
491 /* set obd_namespace to NULL only after invalidate, because we can have
492 * some connect requests in flight, and his need store a connect flags
493 * in obd_namespace. bug 14260 */
494 obd->obd_namespace = NULL;
496 ptlrpc_free_rq_pool(imp->imp_rq_pool);
497 class_destroy_import(imp);
498 cli->cl_import = NULL;
502 err = class_disconnect(exp);
506 up_write(&cli->cl_sem);
508 ldlm_namespace_free_post(to_be_freed);
512 /* --------------------------------------------------------------------------
513 * from old lib/target.c
514 * -------------------------------------------------------------------------- */
516 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
517 struct obd_uuid *cluuid)
520 if (exp->exp_connection && exp->exp_imp_reverse) {
521 struct lustre_handle *hdl;
522 hdl = &exp->exp_imp_reverse->imp_remote_handle;
523 /* Might be a re-connect after a partition. */
524 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
525 CWARN("%s: %s reconnecting\n", exp->exp_obd->obd_name,
527 conn->cookie = exp->exp_handle.h_cookie;
528 /* target_handle_connect() treats EALREADY and
529 * -EALREADY differently. EALREADY means we are
530 * doing a valid reconnect from the same client. */
533 CERROR("%s reconnecting from %s, "
534 "handle mismatch (ours "LPX64", theirs "
535 LPX64")\n", cluuid->uuid,
536 exp->exp_connection->c_remote_uuid.uuid,
537 hdl->cookie, conn->cookie);
538 memset(conn, 0, sizeof *conn);
539 /* target_handle_connect() treats EALREADY and
540 * -EALREADY differently. -EALREADY is an error
541 * (same UUID, different handle). */
546 conn->cookie = exp->exp_handle.h_cookie;
547 CDEBUG(D_HA, "connect export for UUID '%s' at %p, cookie "LPX64"\n",
548 cluuid->uuid, exp, conn->cookie);
552 void target_client_add_cb(struct obd_device *obd, __u64 transno, void *cb_data,
555 struct obd_export *exp = cb_data;
557 CDEBUG(D_RPCTRACE, "%s: committing for initial connect of %s\n",
558 obd->obd_name, exp->exp_client_uuid.uuid);
560 spin_lock(&exp->exp_lock);
561 exp->exp_need_sync = 0;
562 spin_unlock(&exp->exp_lock);
564 EXPORT_SYMBOL(target_client_add_cb);
567 target_start_and_reset_recovery_timer(struct obd_device *obd,
568 svc_handler_t handler,
569 struct ptlrpc_request *req,
572 int target_handle_connect(struct ptlrpc_request *req, svc_handler_t handler)
574 struct obd_device *target, *targref = NULL;
575 struct obd_export *export = NULL;
576 struct obd_import *revimp;
577 struct lustre_handle conn;
578 struct obd_uuid tgtuuid;
579 struct obd_uuid cluuid;
580 struct obd_uuid remote_uuid;
582 int rc = 0, abort_recovery;
583 struct obd_connect_data *data;
584 int size[2] = { sizeof(struct ptlrpc_body), sizeof(*data) };
585 lnet_nid_t *client_nid = NULL;
588 OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
590 lustre_set_req_swabbed(req, REQ_REC_OFF);
591 str = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF, sizeof(tgtuuid)-1);
593 DEBUG_REQ(D_ERROR, req, "bad target UUID for connect");
594 GOTO(out, rc = -EINVAL);
597 obd_str2uuid (&tgtuuid, str);
598 target = class_uuid2obd(&tgtuuid);
600 /* old (pre 1.6) lustre_process_log tries to connect to mdsname
601 (eg. mdsA) instead of uuid. */
603 snprintf((char *)tgtuuid.uuid, sizeof(tgtuuid), "%s_UUID", str);
604 target = class_uuid2obd(&tgtuuid);
607 target = class_name2obd(str);
610 if (!target || target->obd_stopping || !target->obd_set_up) {
611 LCONSOLE_ERROR_MSG(0x137, "UUID '%s' is not available "
612 " for connect (%s)\n", str,
613 !target ? "no target" :
614 (target->obd_stopping ? "stopping" :
616 GOTO(out, rc = -ENODEV);
619 if (target->obd_no_conn) {
620 LCONSOLE_WARN("%s: temporarily refusing client connection "
621 "from %s\n", target->obd_name,
622 libcfs_nid2str(req->rq_peer.nid));
623 GOTO(out, rc = -EAGAIN);
626 /* Make sure the target isn't cleaned up while we're here. Yes,
627 there's still a race between the above check and our incref here.
628 Really, class_uuid2obd should take the ref. */
629 targref = class_incref(target);
631 lustre_set_req_swabbed(req, REQ_REC_OFF + 1);
632 str = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF + 1,
635 DEBUG_REQ(D_ERROR, req, "bad client UUID for connect");
636 GOTO(out, rc = -EINVAL);
639 obd_str2uuid (&cluuid, str);
641 /* XXX extract a nettype and format accordingly */
642 switch (sizeof(lnet_nid_t)) {
643 /* NB the casts only avoid compiler warnings */
645 snprintf(remote_uuid.uuid, sizeof remote_uuid,
646 "NET_"LPX64"_UUID", (__u64)req->rq_peer.nid);
649 snprintf(remote_uuid.uuid, sizeof remote_uuid,
650 "NET_%x_UUID", (__u32)req->rq_peer.nid);
656 spin_lock_bh(&target->obd_processing_task_lock);
657 abort_recovery = target->obd_abort_recovery;
658 spin_unlock_bh(&target->obd_processing_task_lock);
660 target_abort_recovery(target);
662 tmp = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 2, sizeof conn);
664 GOTO(out, rc = -EPROTO);
666 memcpy(&conn, tmp, sizeof conn);
668 data = lustre_swab_reqbuf(req, REQ_REC_OFF + 3, sizeof(*data),
669 lustre_swab_connect);
670 rc = lustre_pack_reply(req, 2, size, NULL);
674 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
676 DEBUG_REQ(D_WARNING, req, "Refusing old (unversioned) "
677 "libclient connection attempt");
678 GOTO(out, rc = -EPROTO);
679 } else if (data->ocd_version < LUSTRE_VERSION_CODE -
680 LUSTRE_VERSION_ALLOWED_OFFSET ||
681 data->ocd_version > LUSTRE_VERSION_CODE +
682 LUSTRE_VERSION_ALLOWED_OFFSET) {
683 DEBUG_REQ(D_WARNING, req, "Refusing %s (%d.%d.%d.%d) "
684 "libclient connection attempt",
685 data->ocd_version < LUSTRE_VERSION_CODE ?
687 OBD_OCD_VERSION_MAJOR(data->ocd_version),
688 OBD_OCD_VERSION_MINOR(data->ocd_version),
689 OBD_OCD_VERSION_PATCH(data->ocd_version),
690 OBD_OCD_VERSION_FIX(data->ocd_version));
691 data = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
692 offsetof(typeof(*data),
694 sizeof(data->ocd_version));
696 data->ocd_connect_flags = OBD_CONNECT_VERSION;
697 data->ocd_version = LUSTRE_VERSION_CODE;
699 GOTO(out, rc = -EPROTO);
703 /* lctl gets a backstage, all-access pass. */
704 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
705 goto dont_check_exports;
707 spin_lock(&target->obd_dev_lock);
708 export = lustre_hash_get_object_by_key(target->obd_uuid_hash_body, &cluuid);
710 if (export != NULL && export->exp_connecting) { /* bug 9635, et. al. */
711 CWARN("%s: exp %p already connecting\n",
712 export->exp_obd->obd_name, export);
713 class_export_put(export);
716 } else if (export != NULL && export->exp_connection != NULL &&
717 req->rq_peer.nid != export->exp_connection->c_peer.nid) {
718 /* make darn sure this is coming from the same peer
719 * if the UUIDs matched */
720 CWARN("%s: cookie %s seen on new NID %s when "
721 "existing NID %s is already connected\n",
722 target->obd_name, cluuid.uuid,
723 libcfs_nid2str(req->rq_peer.nid),
724 libcfs_nid2str(export->exp_connection->c_peer.nid));
725 class_export_put(export);
728 } else if (export != NULL && export->exp_failed) { /* bug 11327 */
729 CDEBUG(D_HA, "%s: exp %p evict in progress - new cookie needed "
730 "for connect\n", export->exp_obd->obd_name, export);
731 class_export_put(export);
734 } else if (export != NULL) {
735 spin_lock(&export->exp_lock);
736 export->exp_connecting = 1;
737 spin_unlock(&export->exp_lock);
738 class_export_put(export);
739 spin_unlock(&target->obd_dev_lock);
740 LASSERT(export->exp_obd == target);
742 rc = target_handle_reconnect(&conn, export, &cluuid);
745 /* If we found an export, we already unlocked. */
747 spin_unlock(&target->obd_dev_lock);
748 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_CONNECT, 2 * obd_timeout);
749 } else if (req->rq_export == NULL &&
750 atomic_read(&export->exp_rpc_count) > 0) {
751 CWARN("%s: refuse connection from %s/%s to 0x%p; still busy "
752 "with %d references\n", target->obd_name, cluuid.uuid,
753 libcfs_nid2str(req->rq_peer.nid),
754 export, atomic_read(&export->exp_refcount));
755 GOTO(out, rc = -EBUSY);
756 } else if (req->rq_export != NULL &&
757 atomic_read(&export->exp_rpc_count) > 1) {
758 CWARN("%s: refuse reconnection from %s@%s to 0x%p; still busy "
759 "with %d active RPCs\n", target->obd_name, cluuid.uuid,
760 libcfs_nid2str(req->rq_peer.nid),
761 export, atomic_read(&export->exp_rpc_count));
762 GOTO(out, rc = -EBUSY);
763 } else if (lustre_msg_get_conn_cnt(req->rq_reqmsg) == 1) {
764 CERROR("%s: NID %s (%s) reconnected with 1 conn_cnt; "
765 "cookies not random?\n", target->obd_name,
766 libcfs_nid2str(req->rq_peer.nid), cluuid.uuid);
767 GOTO(out, rc = -EALREADY);
769 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_RECONNECT, 2 * obd_timeout);
775 /* Tell the client if we're in recovery. */
776 if (target->obd_recovering) {
777 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
778 /* If this is the first time a client connects,
779 reset the recovery timer */
781 target_start_and_reset_recovery_timer(target, handler,
785 /* We want to handle EALREADY but *not* -EALREADY from
786 * target_handle_reconnect(), return reconnection state in a flag */
787 if (rc == EALREADY) {
788 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
794 /* Tell the client if we support replayable requests */
795 if (target->obd_replayable)
796 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
797 client_nid = &req->rq_peer.nid;
799 if (export == NULL) {
800 if (target->obd_recovering) {
801 CERROR("%s: denying connection for new client %s (%s): "
802 "%d clients in recovery for %lds\n",
804 libcfs_nid2str(req->rq_peer.nid), cluuid.uuid,
805 target->obd_recoverable_clients,
806 cfs_duration_sec(cfs_time_sub(cfs_timer_deadline(&target->obd_recovery_timer),
807 cfs_time_current())));
811 rc = obd_connect(&conn, target, &cluuid, data,
815 rc = obd_reconnect(export, target, &cluuid, data);
821 /* Return only the parts of obd_connect_data that we understand, so the
822 * client knows that we don't understand the rest. */
824 memcpy(lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
826 data, sizeof(*data));
828 /* If all else goes well, this is our RPC return code. */
831 lustre_msg_set_handle(req->rq_repmsg, &conn);
833 /* ownership of this export ref transfers to the request AFTER we
834 * drop any previous reference the request had, but we don't want
835 * that to go to zero before we get our new export reference. */
836 export = class_conn2export(&conn);
838 DEBUG_REQ(D_ERROR, req, "Missing export!");
839 GOTO(out, rc = -ENODEV);
842 /* If the client and the server are the same node, we will already
843 * have an export that really points to the client's DLM export,
844 * because we have a shared handles table.
846 * XXX this will go away when shaver stops sending the "connect" handle
847 * in the real "remote handle" field of the request --phik 24 Apr 2003
849 if (req->rq_export != NULL)
850 class_export_put(req->rq_export);
852 req->rq_export = export;
854 spin_lock(&export->exp_lock);
855 if (export->exp_conn_cnt >= lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
856 CERROR("%s: %s already connected at higher conn_cnt: %d > %d\n",
857 cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
858 export->exp_conn_cnt,
859 lustre_msg_get_conn_cnt(req->rq_reqmsg));
861 spin_unlock(&export->exp_lock);
862 GOTO(out, rc = -EALREADY);
864 export->exp_conn_cnt = lustre_msg_get_conn_cnt(req->rq_reqmsg);
866 /* request from liblustre? Don't evict it for not pinging. */
867 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
868 export->exp_libclient = 1;
869 spin_unlock(&export->exp_lock);
871 spin_lock(&target->obd_dev_lock);
872 list_del_init(&export->exp_obd_chain_timed);
873 spin_unlock(&target->obd_dev_lock);
875 spin_unlock(&export->exp_lock);
878 if (export->exp_connection != NULL)
879 ptlrpc_put_connection(export->exp_connection);
880 export->exp_connection = ptlrpc_get_connection(req->rq_peer,
884 spin_lock(&target->obd_dev_lock);
885 /* Export might be hashed already, e.g. if this is reconnect */
886 if (hlist_unhashed(&export->exp_nid_hash))
887 lustre_hash_additem(export->exp_obd->obd_nid_hash_body,
888 &export->exp_connection->c_peer.nid,
889 &export->exp_nid_hash);
890 spin_unlock(&target->obd_dev_lock);
892 if (lustre_msg_get_op_flags(req->rq_repmsg) & MSG_CONNECT_RECONNECT) {
893 revimp = class_import_get(export->exp_imp_reverse);
894 GOTO(set_flags, rc = 0);
897 if (target->obd_recovering)
898 target->obd_connected_clients++;
901 lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 2, sizeof conn),
904 if (export->exp_imp_reverse != NULL)
905 class_destroy_import(export->exp_imp_reverse);
906 revimp = export->exp_imp_reverse = class_new_import(target);
907 revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
908 revimp->imp_client = &export->exp_obd->obd_ldlm_client;
909 revimp->imp_remote_handle = conn;
910 revimp->imp_dlm_fake = 1;
911 revimp->imp_state = LUSTRE_IMP_FULL;
914 if (req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V1 &&
915 lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_NEXT_VER) {
916 revimp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
917 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_NEXT_VER);
919 /* unknown versions will be caught in
920 * ptlrpc_handle_server_req_in->lustre_unpack_msg() */
921 revimp->imp_msg_magic = req->rq_reqmsg->lm_magic;
924 if (revimp->imp_msg_magic != LUSTRE_MSG_MAGIC_V1) {
925 if (export->exp_connect_flags & OBD_CONNECT_AT)
926 revimp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
928 revimp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
931 class_import_put(revimp);
934 spin_lock(&export->exp_lock);
935 export->exp_connecting = 0;
936 spin_unlock(&export->exp_lock);
939 class_decref(targref);
945 int target_handle_disconnect(struct ptlrpc_request *req)
950 rc = lustre_pack_reply(req, 1, NULL, NULL);
954 /* keep the rq_export around so we can send the reply */
955 req->rq_status = obd_disconnect(class_export_get(req->rq_export));
959 void target_destroy_export(struct obd_export *exp)
961 /* exports created from last_rcvd data, and "fake"
962 exports created by lctl don't have an import */
963 if (exp->exp_imp_reverse != NULL)
964 class_destroy_import(exp->exp_imp_reverse);
966 /* We cancel locks at disconnect time, but this will catch any locks
967 * granted in a race with recovery-induced disconnect. */
968 if (exp->exp_obd->obd_namespace != NULL)
969 ldlm_cancel_locks_for_export(exp);
976 static int target_exp_enqueue_req_replay(struct ptlrpc_request *req)
978 __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
979 struct obd_export *exp = req->rq_export;
980 struct ptlrpc_request *reqiter;
985 spin_lock(&exp->exp_lock);
986 list_for_each_entry(reqiter, &exp->exp_req_replay_queue,
988 if (lustre_msg_get_transno(reqiter->rq_reqmsg) == transno) {
995 /* we expect it with RESENT and REPLAY flags */
996 if ((lustre_msg_get_flags(req->rq_reqmsg) &
997 (MSG_RESENT | MSG_REPLAY)) != (MSG_RESENT | MSG_REPLAY))
998 CERROR("invalid flags %x of resent replay\n",
999 lustre_msg_get_flags(req->rq_reqmsg));
1001 list_add_tail(&req->rq_replay_list, &exp->exp_req_replay_queue);
1004 spin_unlock(&exp->exp_lock);
1008 static void target_exp_dequeue_req_replay(struct ptlrpc_request *req)
1010 LASSERT(!list_empty(&req->rq_replay_list));
1011 LASSERT(req->rq_export);
1013 spin_lock(&req->rq_export->exp_lock);
1014 list_del_init(&req->rq_replay_list);
1015 spin_unlock(&req->rq_export->exp_lock);
1018 static void target_release_saved_req(struct ptlrpc_request *req)
1020 ptlrpc_req_drop_rs(req);
1021 class_export_put(req->rq_export);
1022 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
1023 OBD_FREE(req, sizeof *req);
1026 static void target_finish_recovery(struct obd_device *obd)
1028 struct list_head *tmp, *n;
1030 LCONSOLE_INFO("%s: sending delayed replies to recovered clients\n",
1033 ldlm_reprocess_all_ns(obd->obd_namespace);
1035 /* when recovery finished, cleanup orphans on mds and ost */
1036 if (OBT(obd) && OBP(obd, postrecov)) {
1037 int rc = OBP(obd, postrecov)(obd);
1038 LCONSOLE_WARN("%s: recovery %s: rc %d\n", obd->obd_name,
1039 rc < 0 ? "failed" : "complete", rc);
1042 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
1043 struct ptlrpc_request *req;
1044 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1045 list_del(&req->rq_list);
1046 DEBUG_REQ(D_HA, req, "delayed:");
1048 target_release_saved_req(req);
1050 obd->obd_recovery_end = cfs_time_current_sec();
1053 static void abort_recovery_queue(struct obd_device *obd)
1055 struct ptlrpc_request *req;
1056 struct list_head *tmp, *n;
1059 list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
1060 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1061 target_exp_dequeue_req_replay(req);
1062 list_del(&req->rq_list);
1063 DEBUG_REQ(D_ERROR, req, "aborted:");
1064 req->rq_status = -ENOTCONN;
1065 req->rq_type = PTL_RPC_MSG_ERR;
1066 rc = lustre_pack_reply(req, 1, NULL, NULL);
1070 DEBUG_REQ(D_ERROR, req,
1071 "packing failed for abort-reply; skipping");
1072 target_release_saved_req(req);
1076 /* Called from a cleanup function if the device is being cleaned up
1077 forcefully. The exports should all have been disconnected already,
1078 the only thing left to do is
1079 - clear the recovery flags
1081 - free queued requests and replies, but don't send replies
1082 Because the obd_stopping flag is set, no new requests should be received.
1085 void target_cleanup_recovery(struct obd_device *obd)
1087 struct list_head *tmp, *n;
1088 struct ptlrpc_request *req;
1091 LASSERT(obd->obd_stopping);
1093 spin_lock_bh(&obd->obd_processing_task_lock);
1094 if (!obd->obd_recovering) {
1095 spin_unlock_bh(&obd->obd_processing_task_lock);
1099 obd->obd_recovering = obd->obd_abort_recovery = 0;
1100 target_cancel_recovery_timer(obd);
1101 spin_unlock_bh(&obd->obd_processing_task_lock);
1103 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
1104 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1105 list_del(&req->rq_list);
1106 target_release_saved_req(req);
1109 list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
1110 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1111 target_exp_dequeue_req_replay(req);
1112 list_del(&req->rq_list);
1113 target_release_saved_req(req);
1118 void target_abort_recovery(void *data)
1120 struct obd_device *obd = data;
1123 spin_lock_bh(&obd->obd_processing_task_lock);
1124 if (!obd->obd_recovering) {
1125 spin_unlock_bh(&obd->obd_processing_task_lock);
1129 obd->obd_recovering = obd->obd_abort_recovery = 0;
1130 target_cancel_recovery_timer(obd);
1131 spin_unlock_bh(&obd->obd_processing_task_lock);
1133 LCONSOLE_WARN("%s: recovery period over; %d clients never reconnected "
1134 "after %lds (%d clients did)\n",
1135 obd->obd_name, obd->obd_recoverable_clients,
1136 cfs_time_current_sec()- obd->obd_recovery_start,
1137 obd->obd_connected_clients);
1138 class_disconnect_stale_exports(obd);
1139 abort_recovery_queue(obd);
1141 target_finish_recovery(obd);
1142 CDEBUG(D_HA, "%s: recovery complete\n", obd_uuid2str(&obd->obd_uuid));
1146 static void target_recovery_expired(unsigned long castmeharder)
1148 struct obd_device *obd = (struct obd_device *)castmeharder;
1149 CERROR("%s: recovery timed out, aborting\n", obd->obd_name);
1150 spin_lock_bh(&obd->obd_processing_task_lock);
1151 if (obd->obd_recovering)
1152 obd->obd_abort_recovery = 1;
1153 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1154 spin_unlock_bh(&obd->obd_processing_task_lock);
1158 /* obd_processing_task_lock should be held */
1159 void target_cancel_recovery_timer(struct obd_device *obd)
1161 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
1162 cfs_timer_disarm(&obd->obd_recovery_timer);
1165 /* extend = 1 means require at least "duration" seconds left in the timer,
1166 extend = 0 means set the total duration (start_recovery_timer) */
1167 static void reset_recovery_timer(struct obd_device *obd, int duration,
1170 cfs_time_t now = cfs_time_current_sec();
1171 cfs_duration_t left;
1173 spin_lock_bh(&obd->obd_processing_task_lock);
1174 if (!obd->obd_recovering) {
1175 spin_unlock_bh(&obd->obd_processing_task_lock);
1179 left = cfs_time_sub(obd->obd_recovery_end, now);
1181 if (extend && (duration > left))
1182 obd->obd_recovery_timeout += duration - left;
1183 else if (!extend && (duration > obd->obd_recovery_timeout))
1184 /* Track the client's largest expected replay time */
1185 obd->obd_recovery_timeout = duration;
1188 * If total recovery time already exceed the
1189 * obd_recovery_max_time, then CRAY XT3 will
1190 * abort the recovery
1192 if(obd->obd_recovery_timeout > obd->obd_recovery_max_time)
1193 obd->obd_recovery_timeout = obd->obd_recovery_max_time;
1195 obd->obd_recovery_end = obd->obd_recovery_start +
1196 obd->obd_recovery_timeout;
1197 if (cfs_time_before(now, obd->obd_recovery_end)) {
1198 left = cfs_time_sub(obd->obd_recovery_end, now);
1199 cfs_timer_arm(&obd->obd_recovery_timer, cfs_time_shift(left));
1201 spin_unlock_bh(&obd->obd_processing_task_lock);
1202 CDEBUG(D_HA, "%s: recovery timer will expire in %u seconds\n",
1203 obd->obd_name, (unsigned)left);
1206 static void check_and_start_recovery_timer(struct obd_device *obd,
1207 svc_handler_t handler)
1209 spin_lock_bh(&obd->obd_processing_task_lock);
1210 if (obd->obd_recovery_handler) {
1211 spin_unlock_bh(&obd->obd_processing_task_lock);
1214 CWARN("%s: starting recovery timer\n", obd->obd_name);
1215 obd->obd_recovery_start = cfs_time_current_sec();
1217 obd->obd_recovery_timeout = OBD_RECOVERY_FACTOR * obd_timeout;
1218 obd->obd_recovery_handler = handler;
1219 cfs_timer_init(&obd->obd_recovery_timer, target_recovery_expired, obd);
1220 spin_unlock_bh(&obd->obd_processing_task_lock);
1222 reset_recovery_timer(obd, obd->obd_recovery_timeout, 0);
1225 /* Reset the timer with each new client connection */
1227 * This timer is actually reconnect_timer, which is for making sure
1228 * the total recovery window is at least as big as my reconnect
1229 * attempt timing. So the initial recovery time_out will be set to
1230 * OBD_RECOVERY_FACTOR * obd_timeout. If the timeout coming
1231 * from client is bigger than this, then the recovery time_out will
1232 * be extend to make sure the client could be reconnected, in the
1233 * process, the timeout from the new client should be ignored.
1237 target_start_and_reset_recovery_timer(struct obd_device *obd,
1238 svc_handler_t handler,
1239 struct ptlrpc_request *req,
1242 int req_timeout = OBD_RECOVERY_FACTOR *
1243 lustre_msg_get_timeout(req->rq_reqmsg);
1245 check_and_start_recovery_timer(obd, handler);
1247 if (req_timeout > obd->obd_recovery_timeout && !new_client)
1248 reset_recovery_timer(obd, req_timeout, 0);
1251 static int check_for_next_transno(struct obd_device *obd)
1253 struct ptlrpc_request *req;
1254 int wake_up = 0, connected, completed, queue_len, max;
1255 __u64 next_transno, req_transno;
1257 spin_lock_bh(&obd->obd_processing_task_lock);
1258 req = list_entry(obd->obd_recovery_queue.next,
1259 struct ptlrpc_request, rq_list);
1260 max = obd->obd_max_recoverable_clients;
1261 req_transno = lustre_msg_get_transno(req->rq_reqmsg);
1262 connected = obd->obd_connected_clients;
1263 completed = max - obd->obd_recoverable_clients;
1264 queue_len = obd->obd_requests_queued_for_recovery;
1265 next_transno = obd->obd_next_recovery_transno;
1267 CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
1268 "req_transno: "LPU64", next_transno: "LPU64"\n",
1269 max, connected, completed, queue_len, req_transno, next_transno);
1270 if (obd->obd_abort_recovery) {
1271 CDEBUG(D_HA, "waking for aborted recovery\n");
1273 } else if (!obd->obd_recovering) {
1274 CDEBUG(D_HA, "waking for completed recovery (?)\n");
1276 } else if (req_transno == next_transno) {
1277 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1279 } else if (queue_len + completed == max) {
1281 "waking for skipped transno (skip: "LPD64
1282 ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
1283 next_transno, queue_len, completed, max, req_transno);
1284 obd->obd_next_recovery_transno = req_transno;
1287 spin_unlock_bh(&obd->obd_processing_task_lock);
1288 LASSERT(lustre_msg_get_transno(req->rq_reqmsg) >= next_transno);
1292 static void process_recovery_queue(struct obd_device *obd)
1294 struct ptlrpc_request *req;
1295 int abort_recovery = 0;
1296 struct l_wait_info lwi = { 0 };
1300 spin_lock_bh(&obd->obd_processing_task_lock);
1301 LASSERT(obd->obd_processing_task == cfs_curproc_pid());
1302 req = list_entry(obd->obd_recovery_queue.next,
1303 struct ptlrpc_request, rq_list);
1305 if (lustre_msg_get_transno(req->rq_reqmsg) !=
1306 obd->obd_next_recovery_transno) {
1307 spin_unlock_bh(&obd->obd_processing_task_lock);
1308 CDEBUG(D_HA, "Waiting for transno "LPD64" (1st is "
1309 LPD64", x"LPU64")\n",
1310 obd->obd_next_recovery_transno,
1311 lustre_msg_get_transno(req->rq_reqmsg),
1313 l_wait_event(obd->obd_next_transno_waitq,
1314 check_for_next_transno(obd), &lwi);
1315 spin_lock_bh(&obd->obd_processing_task_lock);
1316 abort_recovery = obd->obd_abort_recovery;
1317 spin_unlock_bh(&obd->obd_processing_task_lock);
1318 if (abort_recovery) {
1319 target_abort_recovery(obd);
1324 target_exp_dequeue_req_replay(req);
1325 list_del_init(&req->rq_list);
1326 obd->obd_requests_queued_for_recovery--;
1327 spin_unlock_bh(&obd->obd_processing_task_lock);
1329 DEBUG_REQ(D_HA, req, "processing: ");
1330 (void)obd->obd_recovery_handler(req);
1331 obd->obd_replayed_requests++;
1332 reset_recovery_timer(obd, OBD_RECOVERY_FACTOR *
1333 AT_OFF ? obd_timeout :
1334 at_get(&req->rq_rqbd->rqbd_service->srv_at_estimate), 1);
1335 /* bug 1580: decide how to properly sync() in recovery */
1336 //mds_fsync_super(obd->u.obt.obt_sb);
1337 class_export_put(req->rq_export);
1338 ptlrpc_req_drop_rs(req);
1339 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
1340 OBD_FREE(req, sizeof *req);
1341 spin_lock_bh(&obd->obd_processing_task_lock);
1342 obd->obd_next_recovery_transno++;
1343 if (list_empty(&obd->obd_recovery_queue)) {
1344 obd->obd_processing_task = 0;
1345 spin_unlock_bh(&obd->obd_processing_task_lock);
1348 spin_unlock_bh(&obd->obd_processing_task_lock);
1353 int target_queue_recovery_request(struct ptlrpc_request *req,
1354 struct obd_device *obd)
1356 struct list_head *tmp;
1358 __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
1359 struct ptlrpc_request *saved_req;
1360 struct lustre_msg *reqmsg;
1363 /* CAVEAT EMPTOR: The incoming request message has been swabbed
1364 * (i.e. buflens etc are in my own byte order), but type-dependent
1365 * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1368 CFS_INIT_LIST_HEAD(&req->rq_list);
1369 DEBUG_REQ(D_HA, req, "not queueing");
1373 /* XXX If I were a real man, these LBUGs would be sane cleanups. */
1374 /* XXX just like the request-dup code in queue_final_reply */
1375 OBD_ALLOC(saved_req, sizeof *saved_req);
1378 OBD_ALLOC(reqmsg, req->rq_reqlen);
1382 spin_lock_bh(&obd->obd_processing_task_lock);
1384 /* If we're processing the queue, we want don't want to queue this
1387 * Also, if this request has a transno less than the one we're waiting
1388 * for, we should process it now. It could (and currently always will)
1389 * be an open request for a descriptor that was opened some time ago.
1391 * Also, a resent, replayed request that has already been
1392 * handled will pass through here and be processed immediately.
1394 if (obd->obd_processing_task == cfs_curproc_pid() ||
1395 transno < obd->obd_next_recovery_transno) {
1396 /* Processing the queue right now, don't re-add. */
1397 LASSERT(list_empty(&req->rq_list));
1398 spin_unlock_bh(&obd->obd_processing_task_lock);
1399 GOTO(err_free, rc = 1);
1402 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_TGT_REPLAY_DROP))) {
1403 spin_unlock_bh(&obd->obd_processing_task_lock);
1404 GOTO(err_free, rc = 0);
1407 memcpy(saved_req, req, sizeof *req);
1408 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1410 req->rq_reqmsg = reqmsg;
1411 class_export_get(req->rq_export);
1412 CFS_INIT_LIST_HEAD(&req->rq_list);
1413 CFS_INIT_LIST_HEAD(&req->rq_replay_list);
1415 if (target_exp_enqueue_req_replay(req)) {
1416 spin_unlock_bh(&obd->obd_processing_task_lock);
1417 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1418 GOTO(err_exp, rc = 0);
1422 list_for_each(tmp, &obd->obd_recovery_queue) {
1423 struct ptlrpc_request *reqiter =
1424 list_entry(tmp, struct ptlrpc_request, rq_list);
1426 if (lustre_msg_get_transno(reqiter->rq_reqmsg) > transno) {
1427 list_add_tail(&req->rq_list, &reqiter->rq_list);
1432 if (unlikely(lustre_msg_get_transno(reqiter->rq_reqmsg) ==
1434 spin_unlock_bh(&obd->obd_processing_task_lock);
1435 DEBUG_REQ(D_ERROR, req, "dropping replay: transno "
1436 "has been claimed by another client");
1437 target_exp_dequeue_req_replay(req);
1438 GOTO(err_exp, rc = 0);
1443 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
1446 obd->obd_requests_queued_for_recovery++;
1448 if (obd->obd_processing_task != 0) {
1449 /* Someone else is processing this queue, we'll leave it to
1452 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1453 spin_unlock_bh(&obd->obd_processing_task_lock);
1457 /* Nobody is processing, and we know there's (at least) one to process
1458 * now, so we'll do the honours.
1460 obd->obd_processing_task = cfs_curproc_pid();
1461 spin_unlock_bh(&obd->obd_processing_task_lock);
1463 process_recovery_queue(obd);
1467 class_export_put(req->rq_export);
1469 OBD_FREE(reqmsg, req->rq_reqlen);
1470 OBD_FREE(saved_req, sizeof(*saved_req));
1474 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1476 return req->rq_export->exp_obd;
1479 int target_queue_last_replay_reply(struct ptlrpc_request *req, int rc)
1481 struct obd_device *obd = target_req2obd(req);
1482 struct ptlrpc_request *saved_req;
1483 struct lustre_msg *reqmsg;
1484 int recovery_done = 0;
1486 LASSERT ((rc == 0) == req->rq_packed_final);
1488 if (!req->rq_packed_final) {
1489 /* Just like ptlrpc_error, but without the sending. */
1490 rc = lustre_pack_reply(req, 1, NULL, NULL);
1493 req->rq_type = PTL_RPC_MSG_ERR;
1496 LASSERT(!req->rq_reply_state->rs_difficult);
1497 LASSERT(list_empty(&req->rq_list));
1498 /* XXX a bit like the request-dup code in queue_recovery_request */
1499 OBD_ALLOC(saved_req, sizeof *saved_req);
1502 OBD_ALLOC(reqmsg, req->rq_reqlen);
1504 OBD_FREE(saved_req, sizeof *req);
1508 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1510 /* Don't race cleanup */
1511 spin_lock_bh(&obd->obd_processing_task_lock);
1512 if (obd->obd_stopping) {
1513 spin_unlock_bh(&obd->obd_processing_task_lock);
1514 OBD_FREE(reqmsg, req->rq_reqlen);
1515 OBD_FREE(saved_req, sizeof *req);
1516 req->rq_status = -ENOTCONN;
1517 /* rv is ignored anyhow */
1520 ptlrpc_rs_addref(req->rq_reply_state); /* +1 ref for saved reply */
1522 req->rq_reqmsg = reqmsg;
1523 class_export_get(req->rq_export);
1524 list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
1526 /* only count the first "replay over" request from each
1528 if (req->rq_export->exp_replay_needed) {
1529 --obd->obd_recoverable_clients;
1531 spin_lock(&req->rq_export->exp_lock);
1532 req->rq_export->exp_replay_needed = 0;
1533 spin_unlock(&req->rq_export->exp_lock);
1535 recovery_done = (obd->obd_recoverable_clients == 0);
1536 spin_unlock_bh(&obd->obd_processing_task_lock);
1538 OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
1539 if (recovery_done) {
1540 spin_lock_bh(&obd->obd_processing_task_lock);
1541 obd->obd_recovering = obd->obd_abort_recovery = 0;
1542 target_cancel_recovery_timer(obd);
1543 spin_unlock_bh(&obd->obd_processing_task_lock);
1545 target_finish_recovery(obd);
1546 CDEBUG(D_HA, "%s: recovery complete\n",
1547 obd_uuid2str(&obd->obd_uuid));
1549 CWARN("%s: %d recoverable clients remain\n",
1550 obd->obd_name, obd->obd_recoverable_clients);
1551 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1557 static inline struct ldlm_pool *ldlm_exp2pl(struct obd_export *exp)
1559 LASSERT(exp != NULL);
1560 return &exp->exp_obd->obd_namespace->ns_pool;
1563 int target_pack_pool_reply(struct ptlrpc_request *req)
1565 struct obd_device *obd;
1569 * Check that we still have all structures alive as this may
1570 * be some late rpc in shutdown time.
1572 if (unlikely(!req->rq_export || !req->rq_export->exp_obd ||
1573 !exp_connect_lru_resize(req->rq_export))) {
1574 lustre_msg_set_slv(req->rq_repmsg, 0);
1575 lustre_msg_set_limit(req->rq_repmsg, 0);
1580 * OBD is alive here as export is alive, which we checked above.
1582 obd = req->rq_export->exp_obd;
1584 read_lock(&obd->obd_pool_lock);
1585 lustre_msg_set_slv(req->rq_repmsg, obd->obd_pool_slv);
1586 lustre_msg_set_limit(req->rq_repmsg, obd->obd_pool_limit);
1587 read_unlock(&obd->obd_pool_lock);
1593 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1595 if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1596 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1597 DEBUG_REQ(D_ERROR, req, "dropping reply");
1602 DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
1603 req->rq_status = rc;
1604 return (ptlrpc_send_error(req, 1));
1606 DEBUG_REQ(D_NET, req, "sending reply");
1609 return (ptlrpc_send_reply(req, PTLRPC_REPLY_MAYBE_DIFFICULT));
1613 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1616 struct ptlrpc_reply_state *rs;
1617 struct obd_device *obd;
1618 struct obd_export *exp;
1619 struct ptlrpc_service *svc;
1621 svc = req->rq_rqbd->rqbd_service;
1622 rs = req->rq_reply_state;
1623 if (rs == NULL || !rs->rs_difficult) {
1625 target_send_reply_msg (req, rc, fail_id);
1629 /* must be an export if locks saved */
1630 LASSERT (req->rq_export != NULL);
1631 /* req/reply consistent */
1632 LASSERT (rs->rs_service == svc);
1635 LASSERT (!rs->rs_scheduled);
1636 LASSERT (!rs->rs_scheduled_ever);
1637 LASSERT (!rs->rs_handled);
1638 LASSERT (!rs->rs_on_net);
1639 LASSERT (rs->rs_export == NULL);
1640 LASSERT (list_empty(&rs->rs_obd_list));
1641 LASSERT (list_empty(&rs->rs_exp_list));
1643 exp = class_export_get (req->rq_export);
1646 /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1647 rs->rs_scheduled = 1;
1649 rs->rs_xid = req->rq_xid;
1650 rs->rs_transno = req->rq_transno;
1651 rs->rs_export = exp;
1653 spin_lock(&obd->obd_uncommitted_replies_lock);
1655 if (rs->rs_transno > obd->obd_last_committed) {
1656 /* not committed already */
1657 list_add_tail (&rs->rs_obd_list,
1658 &obd->obd_uncommitted_replies);
1661 spin_unlock (&obd->obd_uncommitted_replies_lock);
1662 spin_lock (&exp->exp_lock);
1664 list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1666 spin_unlock(&exp->exp_lock);
1668 netrc = target_send_reply_msg (req, rc, fail_id);
1670 spin_lock(&svc->srv_lock);
1672 svc->srv_n_difficult_replies++;
1675 /* error sending: reply is off the net. Also we need +1
1676 * reply ref until ptlrpc_server_handle_reply() is done
1677 * with the reply state (if the send was successful, there
1678 * would have been +1 ref for the net, which
1679 * reply_out_callback leaves alone) */
1681 ptlrpc_rs_addref(rs);
1682 atomic_inc (&svc->srv_outstanding_replies);
1685 if (!rs->rs_on_net || /* some notifier */
1686 list_empty(&rs->rs_exp_list) || /* completed already */
1687 list_empty(&rs->rs_obd_list)) {
1688 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1689 cfs_waitq_signal (&svc->srv_waitq);
1691 list_add (&rs->rs_list, &svc->srv_active_replies);
1692 rs->rs_scheduled = 0; /* allow notifier to schedule */
1695 spin_unlock(&svc->srv_lock);
1698 int target_handle_ping(struct ptlrpc_request *req)
1700 obd_ping(req->rq_export);
1701 return lustre_pack_reply(req, 1, NULL, NULL);
1704 void target_committed_to_req(struct ptlrpc_request *req)
1706 struct obd_device *obd = req->rq_export->exp_obd;
1708 if (!obd->obd_no_transno && req->rq_repmsg != NULL)
1709 lustre_msg_set_last_committed(req->rq_repmsg,
1710 obd->obd_last_committed);
1712 DEBUG_REQ(D_IOCTL, req, "not sending last_committed update (%d/"
1713 "%d)", obd->obd_no_transno, req->rq_repmsg == NULL);
1715 CDEBUG(D_INFO, "last_committed x"LPU64", this req x"LPU64"\n",
1716 obd->obd_last_committed, req->rq_xid);
1719 EXPORT_SYMBOL(target_committed_to_req);
1721 #ifdef HAVE_QUOTA_SUPPORT
1722 int target_handle_qc_callback(struct ptlrpc_request *req)
1724 struct obd_quotactl *oqctl;
1725 struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
1727 oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1728 lustre_swab_obd_quotactl);
1729 if (oqctl == NULL) {
1730 CERROR("Can't unpack obd_quotactl\n");
1734 cli->cl_qchk_stat = oqctl->qc_stat;
1739 int target_handle_dqacq_callback(struct ptlrpc_request *req)
1742 struct obd_device *obd = req->rq_export->exp_obd;
1743 struct obd_device *master_obd;
1744 struct lustre_quota_ctxt *qctxt;
1745 struct qunit_data *qdata = NULL;
1747 int repsize[2] = { sizeof(struct ptlrpc_body), 0 };
1750 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_DROP_QUOTA_REQ))
1753 repsize[1] = quota_get_qunit_data_size(req->rq_export->
1756 rc = lustre_pack_reply(req, 2, repsize, NULL);
1760 LASSERT(req->rq_export);
1762 /* there are three forms of qunit(historic causes), so we need to
1763 * adjust qunits from slaves to the same form here */
1764 OBD_ALLOC(qdata, sizeof(struct qunit_data));
1767 rc = quota_get_qdata(req, qdata, QUOTA_REQUEST, QUOTA_EXPORT);
1769 CDEBUG(D_ERROR, "Can't unpack qunit_data\n");
1770 GOTO(out, rc = -EPROTO);
1773 /* we use the observer */
1774 LASSERT(obd->obd_observer && obd->obd_observer->obd_observer);
1775 master_obd = obd->obd_observer->obd_observer;
1776 qctxt = &master_obd->u.obt.obt_qctxt;
1778 LASSERT(qctxt->lqc_handler);
1779 rc = qctxt->lqc_handler(master_obd, qdata,
1780 lustre_msg_get_opc(req->rq_reqmsg));
1781 if (rc && rc != -EDQUOT)
1782 CDEBUG(rc == -EBUSY ? D_QUOTA : D_ERROR,
1783 "dqacq failed! (rc:%d)\n", rc);
1784 req->rq_status = rc;
1786 /* there are three forms of qunit(historic causes), so we need to
1787 * adjust the same form to different forms slaves needed */
1788 rc = quota_copy_qdata(req, qdata, QUOTA_REPLY, QUOTA_EXPORT);
1790 CDEBUG(D_ERROR, "Can't pack qunit_data\n");
1791 GOTO(out, rc = -EPROTO);
1794 /* Block the quota req. b=14840 */
1795 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_BLOCK_QUOTA_REQ, obd_timeout);
1797 rc = ptlrpc_reply(req);
1799 OBD_FREE(qdata, sizeof(struct qunit_data));
1803 #endif /* !__KERNEL__ */
1805 #endif /* HAVE_QUOTA_SUPPORT */
1807 ldlm_mode_t lck_compat_array[] = {
1808 [LCK_EX] LCK_COMPAT_EX,
1809 [LCK_PW] LCK_COMPAT_PW,
1810 [LCK_PR] LCK_COMPAT_PR,
1811 [LCK_CW] LCK_COMPAT_CW,
1812 [LCK_CR] LCK_COMPAT_CR,
1813 [LCK_NL] LCK_COMPAT_NL,
1814 [LCK_GROUP] LCK_COMPAT_GROUP