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 sema_init(&cli->cl_sem, 1);
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)
357 ldlm_put_ref(obddev->obd_force);
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 mutex_down(&cli->cl_sem);
375 rc = class_connect(dlm_handle, obd, cluuid);
379 cli->cl_conn_count++;
380 if (cli->cl_conn_count > 1)
382 exp = class_conn2export(dlm_handle);
384 if (obd->obd_namespace != NULL)
385 CERROR("already have namespace!\n");
386 obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
387 LDLM_NAMESPACE_CLIENT,
388 LDLM_NAMESPACE_GREEDY);
389 if (obd->obd_namespace == NULL)
390 GOTO(out_disco, rc = -ENOMEM);
392 imp->imp_dlm_handle = *dlm_handle;
393 rc = ptlrpc_init_import(imp);
397 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);
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 mutex_up(&cli->cl_sem);
433 ldlm_namespace_free_post(to_be_freed, 0);
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 mutex_down(&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);
484 to_be_freed = obd->obd_namespace;
487 rc = ptlrpc_disconnect_import(imp, 0);
489 ptlrpc_invalidate_import(imp);
490 /* set obd_namespace to NULL only after invalidate, because we can have
491 * some connect requests in flight, and his need store a connect flags
492 * in obd_namespace. bug 14260 */
493 obd->obd_namespace = NULL;
495 ptlrpc_free_rq_pool(imp->imp_rq_pool);
496 class_destroy_import(imp);
497 cli->cl_import = NULL;
501 err = class_disconnect(exp);
505 mutex_up(&cli->cl_sem);
507 ldlm_namespace_free_post(to_be_freed, obd->obd_force);
511 /* --------------------------------------------------------------------------
512 * from old lib/target.c
513 * -------------------------------------------------------------------------- */
515 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
516 struct obd_uuid *cluuid)
519 if (exp->exp_connection && exp->exp_imp_reverse) {
520 struct lustre_handle *hdl;
521 hdl = &exp->exp_imp_reverse->imp_remote_handle;
522 /* Might be a re-connect after a partition. */
523 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
524 CWARN("%s: %s reconnecting\n", exp->exp_obd->obd_name,
526 conn->cookie = exp->exp_handle.h_cookie;
527 /* target_handle_connect() treats EALREADY and
528 * -EALREADY differently. EALREADY means we are
529 * doing a valid reconnect from the same client. */
532 CERROR("%s reconnecting from %s, "
533 "handle mismatch (ours "LPX64", theirs "
534 LPX64")\n", cluuid->uuid,
535 exp->exp_connection->c_remote_uuid.uuid,
536 hdl->cookie, conn->cookie);
537 memset(conn, 0, sizeof *conn);
538 /* target_handle_connect() treats EALREADY and
539 * -EALREADY differently. -EALREADY is an error
540 * (same UUID, different handle). */
545 conn->cookie = exp->exp_handle.h_cookie;
546 CDEBUG(D_HA, "connect export for UUID '%s' at %p, cookie "LPX64"\n",
547 cluuid->uuid, exp, conn->cookie);
551 void target_client_add_cb(struct obd_device *obd, __u64 transno, void *cb_data,
554 struct obd_export *exp = cb_data;
556 CDEBUG(D_RPCTRACE, "%s: committing for initial connect of %s\n",
557 obd->obd_name, exp->exp_client_uuid.uuid);
559 spin_lock(&exp->exp_lock);
560 exp->exp_need_sync = 0;
561 spin_unlock(&exp->exp_lock);
563 EXPORT_SYMBOL(target_client_add_cb);
565 int target_handle_connect(struct ptlrpc_request *req, svc_handler_t handler)
567 struct obd_device *target, *targref = NULL;
568 struct obd_export *export = NULL;
569 struct obd_import *revimp;
570 struct lustre_handle conn;
571 struct obd_uuid tgtuuid;
572 struct obd_uuid cluuid;
573 struct obd_uuid remote_uuid;
575 int rc = 0, abort_recovery;
576 struct obd_connect_data *data;
577 int size[2] = { sizeof(struct ptlrpc_body), sizeof(*data) };
578 lnet_nid_t client_nid = 0;
581 OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
583 lustre_set_req_swabbed(req, REQ_REC_OFF);
584 str = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF, sizeof(tgtuuid)-1);
586 DEBUG_REQ(D_ERROR, req, "bad target UUID for connect");
587 GOTO(out, rc = -EINVAL);
590 obd_str2uuid (&tgtuuid, str);
591 target = class_uuid2obd(&tgtuuid);
593 /* old (pre 1.6) lustre_process_log tries to connect to mdsname
594 (eg. mdsA) instead of uuid. */
596 snprintf((char *)tgtuuid.uuid, sizeof(tgtuuid), "%s_UUID", str);
597 target = class_uuid2obd(&tgtuuid);
600 target = class_name2obd(str);
603 if (!target || target->obd_stopping || !target->obd_set_up) {
604 LCONSOLE_ERROR_MSG(0x137, "UUID '%s' is not available "
605 " for connect (%s)\n", str,
606 !target ? "no target" :
607 (target->obd_stopping ? "stopping" :
609 GOTO(out, rc = -ENODEV);
612 if (target->obd_no_conn) {
613 LCONSOLE_WARN("%s: temporarily refusing client connection "
614 "from %s\n", target->obd_name,
615 libcfs_nid2str(req->rq_peer.nid));
616 GOTO(out, rc = -EAGAIN);
619 /* Make sure the target isn't cleaned up while we're here. Yes,
620 there's still a race between the above check and our incref here.
621 Really, class_uuid2obd should take the ref. */
622 targref = class_incref(target);
624 lustre_set_req_swabbed(req, REQ_REC_OFF + 1);
625 str = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF + 1,
628 DEBUG_REQ(D_ERROR, req, "bad client UUID for connect");
629 GOTO(out, rc = -EINVAL);
632 obd_str2uuid (&cluuid, str);
634 /* XXX extract a nettype and format accordingly */
635 switch (sizeof(lnet_nid_t)) {
636 /* NB the casts only avoid compiler warnings */
638 snprintf(remote_uuid.uuid, sizeof remote_uuid,
639 "NET_"LPX64"_UUID", (__u64)req->rq_peer.nid);
642 snprintf(remote_uuid.uuid, sizeof remote_uuid,
643 "NET_%x_UUID", (__u32)req->rq_peer.nid);
649 spin_lock_bh(&target->obd_processing_task_lock);
650 abort_recovery = target->obd_abort_recovery;
651 spin_unlock_bh(&target->obd_processing_task_lock);
653 target_abort_recovery(target);
655 tmp = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 2, sizeof conn);
657 GOTO(out, rc = -EPROTO);
659 memcpy(&conn, tmp, sizeof conn);
661 data = lustre_swab_reqbuf(req, REQ_REC_OFF + 3, sizeof(*data),
662 lustre_swab_connect);
663 rc = lustre_pack_reply(req, 2, size, NULL);
667 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
669 DEBUG_REQ(D_WARNING, req, "Refusing old (unversioned) "
670 "libclient connection attempt");
671 GOTO(out, rc = -EPROTO);
672 } else if (data->ocd_version < LUSTRE_VERSION_CODE -
673 LUSTRE_VERSION_ALLOWED_OFFSET ||
674 data->ocd_version > LUSTRE_VERSION_CODE +
675 LUSTRE_VERSION_ALLOWED_OFFSET) {
676 DEBUG_REQ(D_WARNING, req, "Refusing %s (%d.%d.%d.%d) "
677 "libclient connection attempt",
678 data->ocd_version < LUSTRE_VERSION_CODE ?
680 OBD_OCD_VERSION_MAJOR(data->ocd_version),
681 OBD_OCD_VERSION_MINOR(data->ocd_version),
682 OBD_OCD_VERSION_PATCH(data->ocd_version),
683 OBD_OCD_VERSION_FIX(data->ocd_version));
684 data = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
685 offsetof(typeof(*data),
687 sizeof(data->ocd_version));
689 data->ocd_connect_flags = OBD_CONNECT_VERSION;
690 data->ocd_version = LUSTRE_VERSION_CODE;
692 GOTO(out, rc = -EPROTO);
696 /* lctl gets a backstage, all-access pass. */
697 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
698 goto dont_check_exports;
700 spin_lock(&target->obd_dev_lock);
701 export = lustre_hash_get_object_by_key(target->obd_uuid_hash_body, &cluuid);
703 if (export != NULL && export->exp_connecting) { /* bug 9635, et. al. */
704 CWARN("%s: exp %p already connecting\n",
705 export->exp_obd->obd_name, export);
706 class_export_put(export);
709 } else if (export != NULL && export->exp_connection != NULL &&
710 req->rq_peer.nid != export->exp_connection->c_peer.nid) {
711 /* make darn sure this is coming from the same peer
712 * if the UUIDs matched */
713 CWARN("%s: cookie %s seen on new NID %s when "
714 "existing NID %s is already connected\n",
715 target->obd_name, cluuid.uuid,
716 libcfs_nid2str(req->rq_peer.nid),
717 libcfs_nid2str(export->exp_connection->c_peer.nid));
718 class_export_put(export);
721 } else if (export != NULL && export->exp_failed) { /* bug 11327 */
722 CDEBUG(D_HA, "%s: exp %p evict in progress - new cookie needed "
723 "for connect\n", export->exp_obd->obd_name, export);
724 class_export_put(export);
727 } else if (export != NULL) {
728 spin_lock(&export->exp_lock);
729 export->exp_connecting = 1;
730 spin_unlock(&export->exp_lock);
731 class_export_put(export);
732 spin_unlock(&target->obd_dev_lock);
733 LASSERT(export->exp_obd == target);
735 rc = target_handle_reconnect(&conn, export, &cluuid);
738 /* If we found an export, we already unlocked. */
740 spin_unlock(&target->obd_dev_lock);
741 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_CONNECT, 2 * obd_timeout);
742 } else if (req->rq_export == NULL &&
743 atomic_read(&export->exp_rpc_count) > 0) {
744 CWARN("%s: refuse connection from %s/%s to 0x%p; still busy "
745 "with %d references\n", target->obd_name, cluuid.uuid,
746 libcfs_nid2str(req->rq_peer.nid),
747 export, atomic_read(&export->exp_refcount));
748 GOTO(out, rc = -EBUSY);
749 } else if (req->rq_export != NULL &&
750 atomic_read(&export->exp_rpc_count) > 1) {
751 CWARN("%s: refuse reconnection from %s@%s to 0x%p; still busy "
752 "with %d active RPCs\n", target->obd_name, cluuid.uuid,
753 libcfs_nid2str(req->rq_peer.nid),
754 export, atomic_read(&export->exp_rpc_count));
755 GOTO(out, rc = -EBUSY);
756 } else if (lustre_msg_get_conn_cnt(req->rq_reqmsg) == 1) {
757 CERROR("%s: NID %s (%s) reconnected with 1 conn_cnt; "
758 "cookies not random?\n", target->obd_name,
759 libcfs_nid2str(req->rq_peer.nid), cluuid.uuid);
760 GOTO(out, rc = -EALREADY);
762 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_RECONNECT, 2 * obd_timeout);
768 /* Tell the client if we're in recovery. */
769 if (target->obd_recovering) {
770 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
771 /* If this is the first time a client connects,
772 reset the recovery timer */
774 target_start_recovery_timer(target, handler, req);
777 /* We want to handle EALREADY but *not* -EALREADY from
778 * target_handle_reconnect(), return reconnection state in a flag */
779 if (rc == EALREADY) {
780 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
786 /* Tell the client if we support replayable requests */
787 if (target->obd_replayable)
788 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
789 client_nid = req->rq_peer.nid;
791 if (export == NULL) {
792 if (target->obd_recovering) {
793 CERROR("%s: denying connection for new client %s (%s): "
794 "%d clients in recovery for %lds\n",
796 libcfs_nid2str(req->rq_peer.nid), cluuid.uuid,
797 target->obd_recoverable_clients,
798 cfs_duration_sec(cfs_time_sub(cfs_timer_deadline(&target->obd_recovery_timer),
799 cfs_time_current())));
803 rc = obd_connect(&conn, target, &cluuid, data,
807 rc = obd_reconnect(export, target, &cluuid, data);
813 /* Return only the parts of obd_connect_data that we understand, so the
814 * client knows that we don't understand the rest. */
816 memcpy(lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
818 data, sizeof(*data));
820 /* If all else goes well, this is our RPC return code. */
823 lustre_msg_set_handle(req->rq_repmsg, &conn);
825 /* ownership of this export ref transfers to the request AFTER we
826 * drop any previous reference the request had, but we don't want
827 * that to go to zero before we get our new export reference. */
828 export = class_conn2export(&conn);
830 DEBUG_REQ(D_ERROR, req, "Missing export!");
831 GOTO(out, rc = -ENODEV);
834 /* If the client and the server are the same node, we will already
835 * have an export that really points to the client's DLM export,
836 * because we have a shared handles table.
838 * XXX this will go away when shaver stops sending the "connect" handle
839 * in the real "remote handle" field of the request --phik 24 Apr 2003
841 if (req->rq_export != NULL)
842 class_export_put(req->rq_export);
844 req->rq_export = export;
846 spin_lock(&export->exp_lock);
847 if (export->exp_conn_cnt >= lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
848 CERROR("%s: %s already connected at higher conn_cnt: %d > %d\n",
849 cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
850 export->exp_conn_cnt,
851 lustre_msg_get_conn_cnt(req->rq_reqmsg));
853 spin_unlock(&export->exp_lock);
854 GOTO(out, rc = -EALREADY);
856 export->exp_conn_cnt = lustre_msg_get_conn_cnt(req->rq_reqmsg);
858 /* request from liblustre? Don't evict it for not pinging. */
859 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
860 export->exp_libclient = 1;
861 spin_unlock(&export->exp_lock);
863 spin_lock(&target->obd_dev_lock);
864 list_del_init(&export->exp_obd_chain_timed);
865 spin_unlock(&target->obd_dev_lock);
867 spin_unlock(&export->exp_lock);
870 if (export->exp_connection != NULL)
871 ptlrpc_put_connection(export->exp_connection);
872 export->exp_connection = ptlrpc_get_connection(req->rq_peer,
876 spin_lock(&target->obd_dev_lock);
877 /* Export might be hashed already, e.g. if this is reconnect */
878 if (hlist_unhashed(&export->exp_nid_hash))
879 lustre_hash_additem(export->exp_obd->obd_nid_hash_body,
880 &export->exp_connection->c_peer.nid,
881 &export->exp_nid_hash);
882 spin_unlock(&target->obd_dev_lock);
884 if (lustre_msg_get_op_flags(req->rq_repmsg) & MSG_CONNECT_RECONNECT) {
885 revimp = class_import_get(export->exp_imp_reverse);
886 GOTO(set_flags, rc = 0);
889 if (target->obd_recovering)
890 target->obd_connected_clients++;
893 lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 2, sizeof conn),
896 if (export->exp_imp_reverse != NULL)
897 class_destroy_import(export->exp_imp_reverse);
898 revimp = export->exp_imp_reverse = class_new_import(target);
899 revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
900 revimp->imp_client = &export->exp_obd->obd_ldlm_client;
901 revimp->imp_remote_handle = conn;
902 revimp->imp_dlm_fake = 1;
903 revimp->imp_state = LUSTRE_IMP_FULL;
906 if (req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V1 &&
907 lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_NEXT_VER) {
908 revimp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
909 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_NEXT_VER);
911 /* unknown versions will be caught in
912 * ptlrpc_handle_server_req_in->lustre_unpack_msg() */
913 revimp->imp_msg_magic = req->rq_reqmsg->lm_magic;
916 if (revimp->imp_msg_magic != LUSTRE_MSG_MAGIC_V1) {
917 if (export->exp_connect_flags & OBD_CONNECT_AT)
918 revimp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
921 class_import_put(revimp);
924 spin_lock(&export->exp_lock);
925 export->exp_connecting = 0;
926 spin_unlock(&export->exp_lock);
929 class_decref(targref);
935 int target_handle_disconnect(struct ptlrpc_request *req)
940 rc = lustre_pack_reply(req, 1, NULL, NULL);
944 /* keep the rq_export around so we can send the reply */
945 req->rq_status = obd_disconnect(class_export_get(req->rq_export));
949 void target_destroy_export(struct obd_export *exp)
951 /* exports created from last_rcvd data, and "fake"
952 exports created by lctl don't have an import */
953 if (exp->exp_imp_reverse != NULL)
954 class_destroy_import(exp->exp_imp_reverse);
956 /* We cancel locks at disconnect time, but this will catch any locks
957 * granted in a race with recovery-induced disconnect. */
958 if (exp->exp_obd->obd_namespace != NULL)
959 ldlm_cancel_locks_for_export(exp);
967 static void target_release_saved_req(struct ptlrpc_request *req)
969 ptlrpc_req_drop_rs(req);
970 class_export_put(req->rq_export);
971 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
972 OBD_FREE(req, sizeof *req);
975 static void target_finish_recovery(struct obd_device *obd)
977 struct list_head *tmp, *n;
979 LCONSOLE_INFO("%s: sending delayed replies to recovered clients\n",
982 ldlm_reprocess_all_ns(obd->obd_namespace);
984 /* when recovery finished, cleanup orphans on mds and ost */
985 if (OBT(obd) && OBP(obd, postrecov)) {
986 int rc = OBP(obd, postrecov)(obd);
987 LCONSOLE_WARN("%s: recovery %s: rc %d\n", obd->obd_name,
988 rc < 0 ? "failed" : "complete", rc);
991 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
992 struct ptlrpc_request *req;
993 req = list_entry(tmp, struct ptlrpc_request, rq_list);
994 list_del(&req->rq_list);
995 DEBUG_REQ(D_HA, req, "delayed:");
997 target_release_saved_req(req);
999 obd->obd_recovery_end = cfs_time_current_sec();
1002 static void abort_recovery_queue(struct obd_device *obd)
1004 struct ptlrpc_request *req;
1005 struct list_head *tmp, *n;
1008 list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
1009 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1010 list_del(&req->rq_list);
1011 DEBUG_REQ(D_ERROR, req, "aborted:");
1012 req->rq_status = -ENOTCONN;
1013 req->rq_type = PTL_RPC_MSG_ERR;
1014 rc = lustre_pack_reply(req, 1, NULL, NULL);
1018 DEBUG_REQ(D_ERROR, req,
1019 "packing failed for abort-reply; skipping");
1020 target_release_saved_req(req);
1024 /* Called from a cleanup function if the device is being cleaned up
1025 forcefully. The exports should all have been disconnected already,
1026 the only thing left to do is
1027 - clear the recovery flags
1029 - free queued requests and replies, but don't send replies
1030 Because the obd_stopping flag is set, no new requests should be received.
1033 void target_cleanup_recovery(struct obd_device *obd)
1035 struct list_head *tmp, *n;
1036 struct ptlrpc_request *req;
1039 LASSERT(obd->obd_stopping);
1041 spin_lock_bh(&obd->obd_processing_task_lock);
1042 if (!obd->obd_recovering) {
1043 spin_unlock_bh(&obd->obd_processing_task_lock);
1047 obd->obd_recovering = obd->obd_abort_recovery = 0;
1048 target_cancel_recovery_timer(obd);
1049 spin_unlock_bh(&obd->obd_processing_task_lock);
1051 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
1052 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1053 list_del(&req->rq_list);
1054 target_release_saved_req(req);
1057 list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
1058 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1059 list_del(&req->rq_list);
1060 target_release_saved_req(req);
1065 void target_abort_recovery(void *data)
1067 struct obd_device *obd = data;
1070 spin_lock_bh(&obd->obd_processing_task_lock);
1071 if (!obd->obd_recovering) {
1072 spin_unlock_bh(&obd->obd_processing_task_lock);
1076 obd->obd_recovering = obd->obd_abort_recovery = 0;
1077 target_cancel_recovery_timer(obd);
1078 spin_unlock_bh(&obd->obd_processing_task_lock);
1080 LCONSOLE_WARN("%s: recovery period over; %d clients never reconnected "
1081 "after %lds (%d clients did)\n",
1082 obd->obd_name, obd->obd_recoverable_clients,
1083 cfs_time_current_sec()- obd->obd_recovery_start,
1084 obd->obd_connected_clients);
1085 class_disconnect_stale_exports(obd);
1086 abort_recovery_queue(obd);
1088 target_finish_recovery(obd);
1089 CDEBUG(D_HA, "%s: recovery complete\n", obd_uuid2str(&obd->obd_uuid));
1093 static void target_recovery_expired(unsigned long castmeharder)
1095 struct obd_device *obd = (struct obd_device *)castmeharder;
1096 CERROR("%s: recovery timed out, aborting\n", obd->obd_name);
1097 spin_lock_bh(&obd->obd_processing_task_lock);
1098 if (obd->obd_recovering)
1099 obd->obd_abort_recovery = 1;
1100 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1101 spin_unlock_bh(&obd->obd_processing_task_lock);
1105 /* obd_processing_task_lock should be held */
1106 void target_cancel_recovery_timer(struct obd_device *obd)
1108 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
1109 cfs_timer_disarm(&obd->obd_recovery_timer);
1112 static void reset_recovery_timer(struct obd_device *obd, int mintime)
1114 spin_lock_bh(&obd->obd_processing_task_lock);
1115 if (!obd->obd_recovering) {
1116 spin_unlock_bh(&obd->obd_processing_task_lock);
1119 /* Track the client's largest expected replay time */
1120 obd->obd_recovery_timeout = max(mintime, obd->obd_recovery_timeout);
1122 if(cfs_time_current_sec() + obd->obd_recovery_timeout >
1123 obd->obd_recovery_start + obd->obd_recovery_max_time)
1124 obd->obd_recovery_timeout = obd->obd_recovery_start +
1125 obd->obd_recovery_max_time - cfs_time_current_sec();
1127 obd->obd_recovery_end = cfs_time_current_sec() +
1128 obd->obd_recovery_timeout;
1129 cfs_timer_arm(&obd->obd_recovery_timer,
1130 cfs_time_shift(obd->obd_recovery_timeout));
1131 spin_unlock_bh(&obd->obd_processing_task_lock);
1132 CDEBUG(D_HA, "%s: recovery timer will expire in %u seconds\n",
1133 obd->obd_name, obd->obd_recovery_timeout);
1137 /* Reset the timer with each new client connection */
1138 void target_start_recovery_timer(struct obd_device *obd, svc_handler_t handler,
1139 struct ptlrpc_request *req)
1141 spin_lock_bh(&obd->obd_processing_task_lock);
1142 if (obd->obd_recovery_handler)
1144 CWARN("%s: starting recovery timer\n", obd->obd_name);
1145 obd->obd_recovery_start = cfs_time_current_sec();
1147 obd->obd_recovery_timeout = OBD_RECOVERY_FACTOR * obd_timeout;
1148 obd->obd_recovery_handler = handler;
1149 cfs_timer_init(&obd->obd_recovery_timer, target_recovery_expired, obd);
1151 spin_unlock_bh(&obd->obd_processing_task_lock);
1152 reset_recovery_timer(obd, OBD_RECOVERY_FACTOR *
1153 lustre_msg_get_timeout(req->rq_reqmsg));
1156 static int check_for_next_transno(struct obd_device *obd)
1158 struct ptlrpc_request *req;
1159 int wake_up = 0, connected, completed, queue_len, max;
1160 __u64 next_transno, req_transno;
1162 spin_lock_bh(&obd->obd_processing_task_lock);
1163 req = list_entry(obd->obd_recovery_queue.next,
1164 struct ptlrpc_request, rq_list);
1165 max = obd->obd_max_recoverable_clients;
1166 req_transno = lustre_msg_get_transno(req->rq_reqmsg);
1167 connected = obd->obd_connected_clients;
1168 completed = max - obd->obd_recoverable_clients;
1169 queue_len = obd->obd_requests_queued_for_recovery;
1170 next_transno = obd->obd_next_recovery_transno;
1172 CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
1173 "req_transno: "LPU64", next_transno: "LPU64"\n",
1174 max, connected, completed, queue_len, req_transno, next_transno);
1175 if (obd->obd_abort_recovery) {
1176 CDEBUG(D_HA, "waking for aborted recovery\n");
1178 } else if (!obd->obd_recovering) {
1179 CDEBUG(D_HA, "waking for completed recovery (?)\n");
1181 } else if (req_transno == next_transno) {
1182 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1184 } else if (queue_len + completed == max) {
1186 "waking for skipped transno (skip: "LPD64
1187 ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
1188 next_transno, queue_len, completed, max, req_transno);
1189 obd->obd_next_recovery_transno = req_transno;
1192 spin_unlock_bh(&obd->obd_processing_task_lock);
1193 LASSERT(lustre_msg_get_transno(req->rq_reqmsg) >= next_transno);
1197 static void process_recovery_queue(struct obd_device *obd)
1199 struct ptlrpc_request *req;
1200 int abort_recovery = 0;
1201 struct l_wait_info lwi = { 0 };
1205 spin_lock_bh(&obd->obd_processing_task_lock);
1206 LASSERT(obd->obd_processing_task == cfs_curproc_pid());
1207 req = list_entry(obd->obd_recovery_queue.next,
1208 struct ptlrpc_request, rq_list);
1210 if (lustre_msg_get_transno(req->rq_reqmsg) !=
1211 obd->obd_next_recovery_transno) {
1212 spin_unlock_bh(&obd->obd_processing_task_lock);
1213 CDEBUG(D_HA, "Waiting for transno "LPD64" (1st is "
1214 LPD64", x"LPU64")\n",
1215 obd->obd_next_recovery_transno,
1216 lustre_msg_get_transno(req->rq_reqmsg),
1218 l_wait_event(obd->obd_next_transno_waitq,
1219 check_for_next_transno(obd), &lwi);
1220 spin_lock_bh(&obd->obd_processing_task_lock);
1221 abort_recovery = obd->obd_abort_recovery;
1222 spin_unlock_bh(&obd->obd_processing_task_lock);
1223 if (abort_recovery) {
1224 target_abort_recovery(obd);
1229 list_del_init(&req->rq_list);
1230 obd->obd_requests_queued_for_recovery--;
1231 spin_unlock_bh(&obd->obd_processing_task_lock);
1233 DEBUG_REQ(D_HA, req, "processing: ");
1234 (void)obd->obd_recovery_handler(req);
1235 obd->obd_replayed_requests++;
1236 reset_recovery_timer(obd, 2 * /* safety */
1237 at_get(&req->rq_rqbd->rqbd_service->srv_at_estimate));
1238 /* bug 1580: decide how to properly sync() in recovery */
1239 //mds_fsync_super(obd->u.obt.obt_sb);
1240 class_export_put(req->rq_export);
1241 ptlrpc_req_drop_rs(req);
1242 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
1243 OBD_FREE(req, sizeof *req);
1244 spin_lock_bh(&obd->obd_processing_task_lock);
1245 obd->obd_next_recovery_transno++;
1246 if (list_empty(&obd->obd_recovery_queue)) {
1247 obd->obd_processing_task = 0;
1248 spin_unlock_bh(&obd->obd_processing_task_lock);
1251 spin_unlock_bh(&obd->obd_processing_task_lock);
1256 int target_queue_recovery_request(struct ptlrpc_request *req,
1257 struct obd_device *obd)
1259 struct list_head *tmp;
1261 __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
1262 struct ptlrpc_request *saved_req;
1263 struct lustre_msg *reqmsg;
1265 /* CAVEAT EMPTOR: The incoming request message has been swabbed
1266 * (i.e. buflens etc are in my own byte order), but type-dependent
1267 * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1270 CFS_INIT_LIST_HEAD(&req->rq_list);
1271 DEBUG_REQ(D_HA, req, "not queueing");
1275 /* XXX If I were a real man, these LBUGs would be sane cleanups. */
1276 /* XXX just like the request-dup code in queue_final_reply */
1277 OBD_ALLOC(saved_req, sizeof *saved_req);
1280 OBD_ALLOC(reqmsg, req->rq_reqlen);
1284 spin_lock_bh(&obd->obd_processing_task_lock);
1286 /* If we're processing the queue, we want don't want to queue this
1289 * Also, if this request has a transno less than the one we're waiting
1290 * for, we should process it now. It could (and currently always will)
1291 * be an open request for a descriptor that was opened some time ago.
1293 * Also, a resent, replayed request that has already been
1294 * handled will pass through here and be processed immediately.
1296 if (obd->obd_processing_task == cfs_curproc_pid() ||
1297 transno < obd->obd_next_recovery_transno) {
1298 /* Processing the queue right now, don't re-add. */
1299 LASSERT(list_empty(&req->rq_list));
1300 spin_unlock_bh(&obd->obd_processing_task_lock);
1301 OBD_FREE(reqmsg, req->rq_reqlen);
1302 OBD_FREE(saved_req, sizeof *saved_req);
1306 /* A resent, replayed request that is still on the queue; just drop it.
1307 The queued request will handle this. */
1308 if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT|MSG_REPLAY)) ==
1309 (MSG_RESENT | MSG_REPLAY)) {
1310 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1311 spin_unlock_bh(&obd->obd_processing_task_lock);
1312 OBD_FREE(reqmsg, req->rq_reqlen);
1313 OBD_FREE(saved_req, sizeof *saved_req);
1317 memcpy(saved_req, req, sizeof *req);
1318 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1320 req->rq_reqmsg = reqmsg;
1321 class_export_get(req->rq_export);
1322 CFS_INIT_LIST_HEAD(&req->rq_list);
1325 list_for_each(tmp, &obd->obd_recovery_queue) {
1326 struct ptlrpc_request *reqiter =
1327 list_entry(tmp, struct ptlrpc_request, rq_list);
1329 if (lustre_msg_get_transno(reqiter->rq_reqmsg) > transno) {
1330 list_add_tail(&req->rq_list, &reqiter->rq_list);
1337 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
1340 obd->obd_requests_queued_for_recovery++;
1342 if (obd->obd_processing_task != 0) {
1343 /* Someone else is processing this queue, we'll leave it to
1346 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1347 spin_unlock_bh(&obd->obd_processing_task_lock);
1351 /* Nobody is processing, and we know there's (at least) one to process
1352 * now, so we'll do the honours.
1354 obd->obd_processing_task = cfs_curproc_pid();
1355 spin_unlock_bh(&obd->obd_processing_task_lock);
1357 process_recovery_queue(obd);
1361 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1363 return req->rq_export->exp_obd;
1366 int target_queue_last_replay_reply(struct ptlrpc_request *req, int rc)
1368 struct obd_device *obd = target_req2obd(req);
1369 struct ptlrpc_request *saved_req;
1370 struct lustre_msg *reqmsg;
1371 int recovery_done = 0;
1373 LASSERT ((rc == 0) == req->rq_packed_final);
1375 if (!req->rq_packed_final) {
1376 /* Just like ptlrpc_error, but without the sending. */
1377 rc = lustre_pack_reply(req, 1, NULL, NULL);
1380 req->rq_type = PTL_RPC_MSG_ERR;
1383 LASSERT(!req->rq_reply_state->rs_difficult);
1384 LASSERT(list_empty(&req->rq_list));
1385 /* XXX a bit like the request-dup code in queue_recovery_request */
1386 OBD_ALLOC(saved_req, sizeof *saved_req);
1389 OBD_ALLOC(reqmsg, req->rq_reqlen);
1391 OBD_FREE(saved_req, sizeof *req);
1395 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1397 /* Don't race cleanup */
1398 spin_lock_bh(&obd->obd_processing_task_lock);
1399 if (obd->obd_stopping) {
1400 spin_unlock_bh(&obd->obd_processing_task_lock);
1401 OBD_FREE(reqmsg, req->rq_reqlen);
1402 OBD_FREE(saved_req, sizeof *req);
1403 req->rq_status = -ENOTCONN;
1404 /* rv is ignored anyhow */
1407 ptlrpc_rs_addref(req->rq_reply_state); /* +1 ref for saved reply */
1409 req->rq_reqmsg = reqmsg;
1410 class_export_get(req->rq_export);
1411 list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
1413 /* only count the first "replay over" request from each
1415 if (req->rq_export->exp_replay_needed) {
1416 --obd->obd_recoverable_clients;
1418 spin_lock(&req->rq_export->exp_lock);
1419 req->rq_export->exp_replay_needed = 0;
1420 spin_unlock(&req->rq_export->exp_lock);
1422 recovery_done = (obd->obd_recoverable_clients == 0);
1423 spin_unlock_bh(&obd->obd_processing_task_lock);
1425 OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
1426 if (recovery_done) {
1427 spin_lock_bh(&obd->obd_processing_task_lock);
1428 obd->obd_recovering = obd->obd_abort_recovery = 0;
1429 target_cancel_recovery_timer(obd);
1430 spin_unlock_bh(&obd->obd_processing_task_lock);
1432 target_finish_recovery(obd);
1433 CDEBUG(D_HA, "%s: recovery complete\n",
1434 obd_uuid2str(&obd->obd_uuid));
1436 CWARN("%s: %d recoverable clients remain\n",
1437 obd->obd_name, obd->obd_recoverable_clients);
1438 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1444 static inline struct ldlm_pool *ldlm_exp2pl(struct obd_export *exp)
1446 LASSERT(exp != NULL);
1447 return &exp->exp_obd->obd_namespace->ns_pool;
1450 int target_pack_pool_reply(struct ptlrpc_request *req)
1452 struct ldlm_pool *pl;
1455 if (!req->rq_export || !req->rq_export->exp_obd ||
1456 !req->rq_export->exp_obd->obd_namespace ||
1457 !exp_connect_lru_resize(req->rq_export)) {
1458 lustre_msg_set_slv(req->rq_repmsg, 0);
1459 lustre_msg_set_limit(req->rq_repmsg, 0);
1463 pl = ldlm_exp2pl(req->rq_export);
1465 spin_lock(&pl->pl_lock);
1466 LASSERT(ldlm_pool_get_slv(pl) != 0 && ldlm_pool_get_limit(pl) != 0);
1467 lustre_msg_set_slv(req->rq_repmsg, ldlm_pool_get_slv(pl));
1468 lustre_msg_set_limit(req->rq_repmsg, ldlm_pool_get_limit(pl));
1469 spin_unlock(&pl->pl_lock);
1475 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1477 if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1478 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1479 DEBUG_REQ(D_ERROR, req, "dropping reply");
1484 DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
1485 req->rq_status = rc;
1486 return (ptlrpc_error(req));
1488 DEBUG_REQ(D_NET, req, "sending reply");
1491 return (ptlrpc_send_reply(req, PTLRPC_REPLY_MAYBE_DIFFICULT));
1495 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1498 struct ptlrpc_reply_state *rs;
1499 struct obd_device *obd;
1500 struct obd_export *exp;
1501 struct ptlrpc_service *svc;
1503 svc = req->rq_rqbd->rqbd_service;
1504 rs = req->rq_reply_state;
1505 if (rs == NULL || !rs->rs_difficult) {
1507 target_send_reply_msg (req, rc, fail_id);
1511 /* must be an export if locks saved */
1512 LASSERT (req->rq_export != NULL);
1513 /* req/reply consistent */
1514 LASSERT (rs->rs_service == svc);
1517 LASSERT (!rs->rs_scheduled);
1518 LASSERT (!rs->rs_scheduled_ever);
1519 LASSERT (!rs->rs_handled);
1520 LASSERT (!rs->rs_on_net);
1521 LASSERT (rs->rs_export == NULL);
1522 LASSERT (list_empty(&rs->rs_obd_list));
1523 LASSERT (list_empty(&rs->rs_exp_list));
1525 exp = class_export_get (req->rq_export);
1528 /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1529 rs->rs_scheduled = 1;
1531 rs->rs_xid = req->rq_xid;
1532 rs->rs_transno = req->rq_transno;
1533 rs->rs_export = exp;
1535 spin_lock(&obd->obd_uncommitted_replies_lock);
1537 if (rs->rs_transno > obd->obd_last_committed) {
1538 /* not committed already */
1539 list_add_tail (&rs->rs_obd_list,
1540 &obd->obd_uncommitted_replies);
1543 spin_unlock (&obd->obd_uncommitted_replies_lock);
1544 spin_lock (&exp->exp_lock);
1546 list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1548 spin_unlock(&exp->exp_lock);
1550 netrc = target_send_reply_msg (req, rc, fail_id);
1552 spin_lock(&svc->srv_lock);
1554 svc->srv_n_difficult_replies++;
1557 /* error sending: reply is off the net. Also we need +1
1558 * reply ref until ptlrpc_server_handle_reply() is done
1559 * with the reply state (if the send was successful, there
1560 * would have been +1 ref for the net, which
1561 * reply_out_callback leaves alone) */
1563 ptlrpc_rs_addref(rs);
1564 atomic_inc (&svc->srv_outstanding_replies);
1567 if (!rs->rs_on_net || /* some notifier */
1568 list_empty(&rs->rs_exp_list) || /* completed already */
1569 list_empty(&rs->rs_obd_list)) {
1570 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1571 cfs_waitq_signal (&svc->srv_waitq);
1573 list_add (&rs->rs_list, &svc->srv_active_replies);
1574 rs->rs_scheduled = 0; /* allow notifier to schedule */
1577 spin_unlock(&svc->srv_lock);
1580 int target_handle_ping(struct ptlrpc_request *req)
1582 obd_ping(req->rq_export);
1583 return lustre_pack_reply(req, 1, NULL, NULL);
1586 void target_committed_to_req(struct ptlrpc_request *req)
1588 struct obd_device *obd = req->rq_export->exp_obd;
1590 if (!obd->obd_no_transno && req->rq_repmsg != NULL)
1591 lustre_msg_set_last_committed(req->rq_repmsg,
1592 obd->obd_last_committed);
1594 DEBUG_REQ(D_IOCTL, req, "not sending last_committed update (%d/"
1595 "%d)", obd->obd_no_transno, req->rq_repmsg == NULL);
1597 CDEBUG(D_INFO, "last_committed x"LPU64", this req x"LPU64"\n",
1598 obd->obd_last_committed, req->rq_xid);
1601 EXPORT_SYMBOL(target_committed_to_req);
1603 #ifdef HAVE_QUOTA_SUPPORT
1604 int target_handle_qc_callback(struct ptlrpc_request *req)
1606 struct obd_quotactl *oqctl;
1607 struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
1609 oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1610 lustre_swab_obd_quotactl);
1611 if (oqctl == NULL) {
1612 CERROR("Can't unpack obd_quotactl\n");
1616 cli->cl_qchk_stat = oqctl->qc_stat;
1621 int target_handle_dqacq_callback(struct ptlrpc_request *req)
1624 struct obd_device *obd = req->rq_export->exp_obd;
1625 struct obd_device *master_obd;
1626 struct lustre_quota_ctxt *qctxt;
1627 struct qunit_data *qdata = NULL;
1629 int repsize[2] = { sizeof(struct ptlrpc_body), 0 };
1632 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_DROP_QUOTA_REQ))
1635 repsize[1] = quota_get_qunit_data_size(req->rq_export->
1638 rc = lustre_pack_reply(req, 2, repsize, NULL);
1642 LASSERT(req->rq_export);
1644 /* there are three forms of qunit(historic causes), so we need to
1645 * adjust qunits from slaves to the same form here */
1646 OBD_ALLOC(qdata, sizeof(struct qunit_data));
1649 rc = quota_get_qdata(req, qdata, QUOTA_REQUEST, QUOTA_EXPORT);
1651 CDEBUG(D_ERROR, "Can't unpack qunit_data\n");
1652 GOTO(out, rc = -EPROTO);
1655 /* we use the observer */
1656 LASSERT(obd->obd_observer && obd->obd_observer->obd_observer);
1657 master_obd = obd->obd_observer->obd_observer;
1658 qctxt = &master_obd->u.obt.obt_qctxt;
1660 LASSERT(qctxt->lqc_handler);
1661 rc = qctxt->lqc_handler(master_obd, qdata,
1662 lustre_msg_get_opc(req->rq_reqmsg));
1663 if (rc && rc != -EDQUOT)
1664 CDEBUG(rc == -EBUSY ? D_QUOTA : D_ERROR,
1665 "dqacq failed! (rc:%d)\n", rc);
1666 req->rq_status = rc;
1668 /* there are three forms of qunit(historic causes), so we need to
1669 * adjust the same form to different forms slaves needed */
1670 rc = quota_copy_qdata(req, qdata, QUOTA_REPLY, QUOTA_EXPORT);
1672 CDEBUG(D_ERROR, "Can't pack qunit_data\n");
1673 GOTO(out, rc = -EPROTO);
1676 /* Block the quota req. b=14840 */
1677 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_BLOCK_QUOTA_REQ, obd_timeout);
1679 rc = ptlrpc_reply(req);
1681 OBD_FREE(qdata, sizeof(struct qunit_data));
1685 #endif /* !__KERNEL__ */
1687 #endif /* HAVE_QUOTA_SUPPORT */
1689 ldlm_mode_t lck_compat_array[] = {
1690 [LCK_EX] LCK_COMPAT_EX,
1691 [LCK_PW] LCK_COMPAT_PW,
1692 [LCK_PR] LCK_COMPAT_PR,
1693 [LCK_CW] LCK_COMPAT_CW,
1694 [LCK_CR] LCK_COMPAT_CR,
1695 [LCK_NL] LCK_COMPAT_NL,
1696 [LCK_GROUP] LCK_COMPAT_GROUP