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 #ifdef ENABLE_CHECKSUM
269 cli->cl_checksum = 1;
272 /* This value may be changed at connect time in
273 ptlrpc_connect_interpret. */
274 cli->cl_max_pages_per_rpc = min((int)PTLRPC_MAX_BRW_PAGES,
275 (int)(1024 * 1024 >> CFS_PAGE_SHIFT));
277 if (!strcmp(name, LUSTRE_MDC_NAME)) {
278 cli->cl_max_rpcs_in_flight = MDC_MAX_RIF_DEFAULT;
279 } else if (num_physpages >> (20 - CFS_PAGE_SHIFT) <= 128 /* MB */) {
280 cli->cl_max_rpcs_in_flight = 2;
281 } else if (num_physpages >> (20 - CFS_PAGE_SHIFT) <= 256 /* MB */) {
282 cli->cl_max_rpcs_in_flight = 3;
283 } else if (num_physpages >> (20 - CFS_PAGE_SHIFT) <= 512 /* MB */) {
284 cli->cl_max_rpcs_in_flight = 4;
286 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
288 rc = ldlm_get_ref(LDLM_NAMESPACE_CLIENT);
290 CERROR("ldlm_get_ref failed: %d\n", rc);
294 ptlrpc_init_client(rq_portal, rp_portal, name,
295 &obddev->obd_ldlm_client);
297 imp = class_new_import(obddev);
299 GOTO(err_ldlm, rc = -ENOENT);
300 imp->imp_client = &obddev->obd_ldlm_client;
301 imp->imp_connect_op = connect_op;
302 imp->imp_initial_recov = 1;
303 imp->imp_initial_recov_bk = 0;
304 CFS_INIT_LIST_HEAD(&imp->imp_pinger_chain);
305 memcpy(cli->cl_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
306 LUSTRE_CFG_BUFLEN(lcfg, 1));
307 class_import_put(imp);
309 rc = client_import_add_conn(imp, &server_uuid, 1);
311 CERROR("can't add initial connection\n");
312 GOTO(err_import, rc);
315 cli->cl_import = imp;
316 /* cli->cl_max_mds_{easize,cookiesize} updated by mdc_init_ea_size() */
317 cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
318 cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
320 if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
321 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
322 CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
323 name, obddev->obd_name,
324 cli->cl_target_uuid.uuid);
326 spin_lock(&imp->imp_lock);
327 imp->imp_invalid = 1;
328 spin_unlock(&imp->imp_lock);
332 cli->cl_qchk_stat = CL_NOT_QUOTACHECKED;
337 class_destroy_import(imp);
339 ldlm_put_ref(LDLM_NAMESPACE_CLIENT, 0);
345 int client_obd_cleanup(struct obd_device *obddev)
348 ldlm_put_ref(LDLM_NAMESPACE_CLIENT, obddev->obd_force);
352 /* ->o_connect() method for client side (OSC and MDC and MGC) */
353 int client_connect_import(struct lustre_handle *dlm_handle,
354 struct obd_device *obd, struct obd_uuid *cluuid,
355 struct obd_connect_data *data)
357 struct client_obd *cli = &obd->u.cli;
358 struct obd_import *imp = cli->cl_import;
359 struct obd_export *exp;
360 struct obd_connect_data *ocd;
361 struct ldlm_namespace *to_be_freed = NULL;
365 mutex_down(&cli->cl_sem);
366 rc = class_connect(dlm_handle, obd, cluuid);
370 cli->cl_conn_count++;
371 if (cli->cl_conn_count > 1)
373 exp = class_conn2export(dlm_handle);
375 if (obd->obd_namespace != NULL)
376 CERROR("already have namespace!\n");
377 obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
378 LDLM_NAMESPACE_CLIENT,
379 LDLM_NAMESPACE_GREEDY);
380 if (obd->obd_namespace == NULL)
381 GOTO(out_disco, rc = -ENOMEM);
383 imp->imp_dlm_handle = *dlm_handle;
384 rc = ptlrpc_init_import(imp);
388 ocd = &imp->imp_connect_data;
391 imp->imp_connect_flags_orig = data->ocd_connect_flags;
394 rc = ptlrpc_connect_import(imp, NULL);
396 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
399 LASSERT(exp->exp_connection);
402 LASSERT((ocd->ocd_connect_flags & data->ocd_connect_flags) ==
403 ocd->ocd_connect_flags);
404 data->ocd_connect_flags = ocd->ocd_connect_flags;
407 ptlrpc_pinger_add_import(imp);
412 ldlm_namespace_free_prior(obd->obd_namespace);
413 to_be_freed = obd->obd_namespace;
414 obd->obd_namespace = NULL;
416 cli->cl_conn_count--;
417 class_disconnect(exp);
419 class_export_put(exp);
422 mutex_up(&cli->cl_sem);
424 ldlm_namespace_free_post(to_be_freed, 0);
428 int client_disconnect_export(struct obd_export *exp)
430 struct obd_device *obd = class_exp2obd(exp);
431 struct client_obd *cli;
432 struct obd_import *imp;
433 struct ldlm_namespace *to_be_freed = NULL;
438 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
439 exp, exp ? exp->exp_handle.h_cookie : -1);
444 imp = cli->cl_import;
446 mutex_down(&cli->cl_sem);
447 if (!cli->cl_conn_count) {
448 CERROR("disconnecting disconnected device (%s)\n",
450 GOTO(out_sem, rc = -EINVAL);
453 cli->cl_conn_count--;
454 if (cli->cl_conn_count)
455 GOTO(out_no_disconnect, rc = 0);
457 /* Mark import deactivated now, so we don't try to reconnect if any
458 * of the cleanup RPCs fails (e.g. ldlm cancel, etc). We don't
459 * fully deactivate the import, or that would drop all requests. */
460 spin_lock(&imp->imp_lock);
461 imp->imp_deactive = 1;
462 spin_unlock(&imp->imp_lock);
464 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
465 * delete it regardless. (It's safe to delete an import that was
467 (void)ptlrpc_pinger_del_import(imp);
469 if (obd->obd_namespace != NULL) {
470 /* obd_force == local only */
471 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
472 obd->obd_force ? LDLM_FL_LOCAL_ONLY:0,
474 ldlm_namespace_free_prior(obd->obd_namespace);
475 to_be_freed = obd->obd_namespace;
476 obd->obd_namespace = NULL;
479 /* Yeah, obd_force means "forced shutdown". */
481 rc = ptlrpc_disconnect_import(imp, 0);
483 ptlrpc_invalidate_import(imp);
484 ptlrpc_free_rq_pool(imp->imp_rq_pool);
485 class_destroy_import(imp);
486 cli->cl_import = NULL;
490 err = class_disconnect(exp);
494 mutex_up(&cli->cl_sem);
496 ldlm_namespace_free_post(to_be_freed, obd->obd_no_recov);
500 /* --------------------------------------------------------------------------
501 * from old lib/target.c
502 * -------------------------------------------------------------------------- */
504 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
505 struct obd_uuid *cluuid)
508 if (exp->exp_connection && exp->exp_imp_reverse) {
509 struct lustre_handle *hdl;
510 hdl = &exp->exp_imp_reverse->imp_remote_handle;
511 /* Might be a re-connect after a partition. */
512 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
513 CWARN("%s: %s reconnecting\n", exp->exp_obd->obd_name,
515 conn->cookie = exp->exp_handle.h_cookie;
516 /* target_handle_connect() treats EALREADY and
517 * -EALREADY differently. EALREADY means we are
518 * doing a valid reconnect from the same client. */
521 CERROR("%s reconnecting from %s, "
522 "handle mismatch (ours "LPX64", theirs "
523 LPX64")\n", cluuid->uuid,
524 exp->exp_connection->c_remote_uuid.uuid,
525 hdl->cookie, conn->cookie);
526 memset(conn, 0, sizeof *conn);
527 /* target_handle_connect() treats EALREADY and
528 * -EALREADY differently. -EALREADY is an error
529 * (same UUID, different handle). */
534 conn->cookie = exp->exp_handle.h_cookie;
535 CDEBUG(D_HA, "connect export for UUID '%s' at %p, cookie "LPX64"\n",
536 cluuid->uuid, exp, conn->cookie);
540 void target_client_add_cb(struct obd_device *obd, __u64 transno, void *cb_data,
543 struct obd_export *exp = cb_data;
545 CDEBUG(D_RPCTRACE, "%s: committing for initial connect of %s\n",
546 obd->obd_name, exp->exp_client_uuid.uuid);
548 spin_lock(&exp->exp_lock);
549 exp->exp_need_sync = 0;
550 spin_unlock(&exp->exp_lock);
552 EXPORT_SYMBOL(target_client_add_cb);
554 int target_handle_connect(struct ptlrpc_request *req, svc_handler_t handler)
556 struct obd_device *target, *targref = NULL;
557 struct obd_export *export = NULL;
558 struct obd_import *revimp;
559 struct lustre_handle conn;
560 struct obd_uuid tgtuuid;
561 struct obd_uuid cluuid;
562 struct obd_uuid remote_uuid;
564 int rc = 0, abort_recovery;
565 struct obd_connect_data *data;
566 int size[2] = { sizeof(struct ptlrpc_body), sizeof(*data) };
569 OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
571 LASSERT_REQSWAB(req, REQ_REC_OFF);
572 str = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF, sizeof(tgtuuid)-1);
574 DEBUG_REQ(D_ERROR, req, "bad target UUID for connect");
575 GOTO(out, rc = -EINVAL);
578 obd_str2uuid (&tgtuuid, str);
579 target = class_uuid2obd(&tgtuuid);
581 /* old (pre 1.6) lustre_process_log tries to connect to mdsname
582 (eg. mdsA) instead of uuid. */
584 snprintf((char *)tgtuuid.uuid, sizeof(tgtuuid), "%s_UUID", str);
585 target = class_uuid2obd(&tgtuuid);
588 target = class_name2obd(str);
591 if (!target || target->obd_stopping || !target->obd_set_up) {
592 LCONSOLE_ERROR_MSG(0x137, "UUID '%s' is not available "
593 " for connect (%s)\n", str,
594 !target ? "no target" :
595 (target->obd_stopping ? "stopping" :
597 GOTO(out, rc = -ENODEV);
600 if (target->obd_no_conn) {
601 LCONSOLE_WARN("%s: temporarily refusing client connection "
602 "from %s\n", target->obd_name,
603 libcfs_nid2str(req->rq_peer.nid));
604 GOTO(out, rc = -EAGAIN);
607 /* Make sure the target isn't cleaned up while we're here. Yes,
608 there's still a race between the above check and our incref here.
609 Really, class_uuid2obd should take the ref. */
610 targref = class_incref(target);
612 LASSERT_REQSWAB(req, REQ_REC_OFF + 1);
613 str = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF + 1,
616 DEBUG_REQ(D_ERROR, req, "bad client UUID for connect");
617 GOTO(out, rc = -EINVAL);
620 obd_str2uuid (&cluuid, str);
622 /* XXX extract a nettype and format accordingly */
623 switch (sizeof(lnet_nid_t)) {
624 /* NB the casts only avoid compiler warnings */
626 snprintf(remote_uuid.uuid, sizeof remote_uuid,
627 "NET_"LPX64"_UUID", (__u64)req->rq_peer.nid);
630 snprintf(remote_uuid.uuid, sizeof remote_uuid,
631 "NET_%x_UUID", (__u32)req->rq_peer.nid);
637 spin_lock_bh(&target->obd_processing_task_lock);
638 abort_recovery = target->obd_abort_recovery;
639 spin_unlock_bh(&target->obd_processing_task_lock);
641 target_abort_recovery(target);
643 tmp = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 2, sizeof conn);
645 GOTO(out, rc = -EPROTO);
647 memcpy(&conn, tmp, sizeof conn);
649 data = lustre_swab_reqbuf(req, REQ_REC_OFF + 3, sizeof(*data),
650 lustre_swab_connect);
651 rc = lustre_pack_reply(req, 2, size, NULL);
655 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
657 DEBUG_REQ(D_WARNING, req, "Refusing old (unversioned) "
658 "libclient connection attempt");
659 GOTO(out, rc = -EPROTO);
660 } else if (data->ocd_version < LUSTRE_VERSION_CODE -
661 LUSTRE_VERSION_ALLOWED_OFFSET ||
662 data->ocd_version > LUSTRE_VERSION_CODE +
663 LUSTRE_VERSION_ALLOWED_OFFSET) {
664 DEBUG_REQ(D_WARNING, req, "Refusing %s (%d.%d.%d.%d) "
665 "libclient connection attempt",
666 data->ocd_version < LUSTRE_VERSION_CODE ?
668 OBD_OCD_VERSION_MAJOR(data->ocd_version),
669 OBD_OCD_VERSION_MINOR(data->ocd_version),
670 OBD_OCD_VERSION_PATCH(data->ocd_version),
671 OBD_OCD_VERSION_FIX(data->ocd_version));
672 data = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
673 offsetof(typeof(*data),
675 sizeof(data->ocd_version));
677 data->ocd_connect_flags = OBD_CONNECT_VERSION;
678 data->ocd_version = LUSTRE_VERSION_CODE;
680 GOTO(out, rc = -EPROTO);
684 /* lctl gets a backstage, all-access pass. */
685 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
686 goto dont_check_exports;
688 spin_lock(&target->obd_dev_lock);
689 export = lustre_hash_get_object_by_key(target->obd_uuid_hash_body, &cluuid);
691 if (export != NULL && export->exp_connecting) { /* bug 9635, et. al. */
692 CWARN("%s: exp %p already connecting\n",
693 export->exp_obd->obd_name, export);
694 class_export_put(export);
697 } else if (export != NULL && export->exp_connection != NULL &&
698 req->rq_peer.nid != export->exp_connection->c_peer.nid) {
699 /* make darn sure this is coming from the same peer
700 * if the UUIDs matched */
701 CWARN("%s: cookie %s seen on new NID %s when "
702 "existing NID %s is already connected\n",
703 target->obd_name, cluuid.uuid,
704 libcfs_nid2str(req->rq_peer.nid),
705 libcfs_nid2str(export->exp_connection->c_peer.nid));
706 class_export_put(export);
709 } else if (export != NULL && export->exp_failed) { /* bug 11327 */
710 CDEBUG(D_HA, "%s: exp %p evict in progress - new cookie needed "
711 "for connect\n", export->exp_obd->obd_name, export);
712 class_export_put(export);
715 } else if (export != NULL) {
716 spin_lock(&export->exp_lock);
717 export->exp_connecting = 1;
718 spin_unlock(&export->exp_lock);
719 class_export_put(export);
720 spin_unlock(&target->obd_dev_lock);
721 LASSERT(export->exp_obd == target);
723 rc = target_handle_reconnect(&conn, export, &cluuid);
726 /* If we found an export, we already unlocked. */
728 spin_unlock(&target->obd_dev_lock);
729 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_CONNECT, 2 * obd_timeout);
730 } else if (req->rq_export == NULL &&
731 atomic_read(&export->exp_rpc_count) > 0) {
732 CWARN("%s: refuse connection from %s/%s to 0x%p; still busy "
733 "with %d references\n", target->obd_name, cluuid.uuid,
734 libcfs_nid2str(req->rq_peer.nid),
735 export, atomic_read(&export->exp_refcount));
736 GOTO(out, rc = -EBUSY);
737 } else if (req->rq_export != NULL &&
738 atomic_read(&export->exp_rpc_count) > 1) {
739 CWARN("%s: refuse reconnection from %s@%s to 0x%p; still busy "
740 "with %d active RPCs\n", target->obd_name, cluuid.uuid,
741 libcfs_nid2str(req->rq_peer.nid),
742 export, atomic_read(&export->exp_rpc_count));
743 GOTO(out, rc = -EBUSY);
744 } else if (lustre_msg_get_conn_cnt(req->rq_reqmsg) == 1) {
745 CERROR("%s: NID %s (%s) reconnected with 1 conn_cnt; "
746 "cookies not random?\n", target->obd_name,
747 libcfs_nid2str(req->rq_peer.nid), cluuid.uuid);
748 GOTO(out, rc = -EALREADY);
750 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_RECONNECT, 2 * obd_timeout);
753 /* We want to handle EALREADY but *not* -EALREADY from
754 * target_handle_reconnect(), return reconnection state in a flag */
755 if (rc == EALREADY) {
756 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
762 /* Tell the client if we're in recovery. */
763 /* If this is the first client, start the recovery timer */
764 if (target->obd_recovering) {
765 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
766 target_start_recovery_timer(target, handler, req);
769 /* Tell the client if we support replayable requests */
770 if (target->obd_replayable)
771 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
773 if (export == NULL) {
774 if (target->obd_recovering) {
775 CERROR("%s: denying connection for new client %s (%s): "
776 "%d clients in recovery for %lds\n",
778 libcfs_nid2str(req->rq_peer.nid), cluuid.uuid,
779 target->obd_recoverable_clients,
780 cfs_duration_sec(cfs_time_sub(cfs_timer_deadline(&target->obd_recovery_timer),
781 cfs_time_current())));
785 rc = obd_connect(&conn, target, &cluuid, data);
788 rc = obd_reconnect(export, target, &cluuid, data);
794 /* Return only the parts of obd_connect_data that we understand, so the
795 * client knows that we don't understand the rest. */
797 memcpy(lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
799 data, sizeof(*data));
801 /* If all else goes well, this is our RPC return code. */
804 lustre_msg_set_handle(req->rq_repmsg, &conn);
806 /* ownership of this export ref transfers to the request AFTER we
807 * drop any previous reference the request had, but we don't want
808 * that to go to zero before we get our new export reference. */
809 export = class_conn2export(&conn);
811 DEBUG_REQ(D_ERROR, req, "Missing export!");
812 GOTO(out, rc = -ENODEV);
815 /* If the client and the server are the same node, we will already
816 * have an export that really points to the client's DLM export,
817 * because we have a shared handles table.
819 * XXX this will go away when shaver stops sending the "connect" handle
820 * in the real "remote handle" field of the request --phik 24 Apr 2003
822 if (req->rq_export != NULL)
823 class_export_put(req->rq_export);
825 req->rq_export = export;
827 spin_lock(&export->exp_lock);
828 if (export->exp_conn_cnt >= lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
829 CERROR("%s: %s already connected at higher conn_cnt: %d > %d\n",
830 cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
831 export->exp_conn_cnt,
832 lustre_msg_get_conn_cnt(req->rq_reqmsg));
834 spin_unlock(&export->exp_lock);
835 GOTO(out, rc = -EALREADY);
837 export->exp_conn_cnt = lustre_msg_get_conn_cnt(req->rq_reqmsg);
839 /* request from liblustre? Don't evict it for not pinging. */
840 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
841 export->exp_libclient = 1;
842 spin_unlock(&export->exp_lock);
844 spin_lock(&target->obd_dev_lock);
845 list_del_init(&export->exp_obd_chain_timed);
846 spin_unlock(&target->obd_dev_lock);
848 spin_unlock(&export->exp_lock);
851 if (export->exp_connection != NULL)
852 ptlrpc_put_connection(export->exp_connection);
853 export->exp_connection = ptlrpc_get_connection(req->rq_peer,
857 spin_lock(&target->obd_dev_lock);
858 /* Export might be hashed already, e.g. if this is reconnect */
859 if (hlist_unhashed(&export->exp_nid_hash))
860 lustre_hash_additem(export->exp_obd->obd_nid_hash_body,
861 &export->exp_connection->c_peer.nid,
862 &export->exp_nid_hash);
863 spin_unlock(&target->obd_dev_lock);
865 if (lustre_msg_get_op_flags(req->rq_repmsg) & MSG_CONNECT_RECONNECT) {
866 revimp = class_import_get(export->exp_imp_reverse);
867 GOTO(set_flags, rc = 0);
870 if (target->obd_recovering)
871 target->obd_connected_clients++;
874 lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 2, sizeof conn),
877 if (export->exp_imp_reverse != NULL)
878 class_destroy_import(export->exp_imp_reverse);
879 revimp = export->exp_imp_reverse = class_new_import(target);
880 revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
881 revimp->imp_client = &export->exp_obd->obd_ldlm_client;
882 revimp->imp_remote_handle = conn;
883 revimp->imp_dlm_fake = 1;
884 revimp->imp_state = LUSTRE_IMP_FULL;
886 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_NEXT_VER) {
887 /* Client wants v2 */
888 revimp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
889 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_NEXT_VER);
890 if (export->exp_connect_flags & OBD_CONNECT_AT)
891 revimp->imp_msg_flags |= MSG_AT_SUPPORT;
894 class_import_put(revimp);
897 spin_lock(&export->exp_lock);
898 export->exp_connecting = 0;
899 spin_unlock(&export->exp_lock);
902 class_decref(targref);
908 int target_handle_disconnect(struct ptlrpc_request *req)
913 rc = lustre_pack_reply(req, 1, NULL, NULL);
917 /* keep the rq_export around so we can send the reply */
918 req->rq_status = obd_disconnect(class_export_get(req->rq_export));
922 void target_destroy_export(struct obd_export *exp)
924 /* exports created from last_rcvd data, and "fake"
925 exports created by lctl don't have an import */
926 if (exp->exp_imp_reverse != NULL)
927 class_destroy_import(exp->exp_imp_reverse);
929 /* We cancel locks at disconnect time, but this will catch any locks
930 * granted in a race with recovery-induced disconnect. */
931 if (exp->exp_obd->obd_namespace != NULL)
932 ldlm_cancel_locks_for_export(exp);
940 static void target_release_saved_req(struct ptlrpc_request *req)
942 ptlrpc_req_drop_rs(req);
943 class_export_put(req->rq_export);
944 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
945 OBD_FREE(req, sizeof *req);
948 static void target_finish_recovery(struct obd_device *obd)
950 struct list_head *tmp, *n;
952 LCONSOLE_INFO("%s: sending delayed replies to recovered clients\n",
955 ldlm_reprocess_all_ns(obd->obd_namespace);
957 /* when recovery finished, cleanup orphans on mds and ost */
958 if (OBT(obd) && OBP(obd, postrecov)) {
959 int rc = OBP(obd, postrecov)(obd);
960 LCONSOLE_WARN("%s: recovery %s: rc %d\n", obd->obd_name,
961 rc < 0 ? "failed" : "complete", rc);
964 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
965 struct ptlrpc_request *req;
966 req = list_entry(tmp, struct ptlrpc_request, rq_list);
967 list_del(&req->rq_list);
968 DEBUG_REQ(D_HA, req, "delayed:");
970 target_release_saved_req(req);
972 obd->obd_recovery_end = cfs_time_current_sec();
975 static void abort_recovery_queue(struct obd_device *obd)
977 struct ptlrpc_request *req;
978 struct list_head *tmp, *n;
981 list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
982 req = list_entry(tmp, struct ptlrpc_request, rq_list);
983 list_del(&req->rq_list);
984 DEBUG_REQ(D_ERROR, req, "aborted:");
985 req->rq_status = -ENOTCONN;
986 req->rq_type = PTL_RPC_MSG_ERR;
987 rc = lustre_pack_reply(req, 1, NULL, NULL);
991 DEBUG_REQ(D_ERROR, req,
992 "packing failed for abort-reply; skipping");
994 target_release_saved_req(req);
998 /* Called from a cleanup function if the device is being cleaned up
999 forcefully. The exports should all have been disconnected already,
1000 the only thing left to do is
1001 - clear the recovery flags
1003 - free queued requests and replies, but don't send replies
1004 Because the obd_stopping flag is set, no new requests should be received.
1007 void target_cleanup_recovery(struct obd_device *obd)
1009 struct list_head *tmp, *n;
1010 struct ptlrpc_request *req;
1013 LASSERT(obd->obd_stopping);
1015 spin_lock_bh(&obd->obd_processing_task_lock);
1016 if (!obd->obd_recovering) {
1017 spin_unlock_bh(&obd->obd_processing_task_lock);
1021 obd->obd_recovering = obd->obd_abort_recovery = 0;
1022 target_cancel_recovery_timer(obd);
1023 spin_unlock_bh(&obd->obd_processing_task_lock);
1025 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
1026 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1027 list_del(&req->rq_list);
1028 target_release_saved_req(req);
1031 list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
1032 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1033 list_del(&req->rq_list);
1034 target_release_saved_req(req);
1039 void target_abort_recovery(void *data)
1041 struct obd_device *obd = data;
1044 spin_lock_bh(&obd->obd_processing_task_lock);
1045 if (!obd->obd_recovering) {
1046 spin_unlock_bh(&obd->obd_processing_task_lock);
1050 obd->obd_recovering = obd->obd_abort_recovery = 0;
1051 obd->obd_recoverable_clients = 0;
1052 target_cancel_recovery_timer(obd);
1053 spin_unlock_bh(&obd->obd_processing_task_lock);
1055 LCONSOLE_WARN("%s: recovery period over; disconnecting unfinished "
1056 "clients.\n", obd->obd_name);
1057 class_disconnect_stale_exports(obd);
1058 abort_recovery_queue(obd);
1060 target_finish_recovery(obd);
1061 CDEBUG(D_HA, "%s: recovery complete\n", obd_uuid2str(&obd->obd_uuid));
1065 static void target_recovery_expired(unsigned long castmeharder)
1067 struct obd_device *obd = (struct obd_device *)castmeharder;
1068 CERROR("%s: recovery timed out, aborting\n", obd->obd_name);
1069 spin_lock_bh(&obd->obd_processing_task_lock);
1070 if (obd->obd_recovering)
1071 obd->obd_abort_recovery = 1;
1072 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1073 spin_unlock_bh(&obd->obd_processing_task_lock);
1077 /* obd_processing_task_lock should be held */
1078 void target_cancel_recovery_timer(struct obd_device *obd)
1080 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
1081 cfs_timer_disarm(&obd->obd_recovery_timer);
1084 static void reset_recovery_timer(struct obd_device *obd,
1085 struct ptlrpc_request *req, int first)
1087 spin_lock_bh(&obd->obd_processing_task_lock);
1088 if (!obd->obd_recovering) {
1089 spin_unlock_bh(&obd->obd_processing_task_lock);
1092 /* Track the client's largest expected replay time */
1093 obd->obd_recovery_timeout =
1094 max((first ? (int)OBD_RECOVERY_TIMEOUT :
1095 obd->obd_recovery_timeout),
1096 (int)lustre_msg_get_timeout(req->rq_reqmsg));
1097 cfs_timer_arm(&obd->obd_recovery_timer,
1098 cfs_time_shift(obd->obd_recovery_timeout));
1099 spin_unlock_bh(&obd->obd_processing_task_lock);
1100 CDEBUG(D_HA, "%s: recovery timer will expire in %u seconds\n",
1101 obd->obd_name, obd->obd_recovery_timeout);
1102 obd->obd_recovery_end = cfs_time_current_sec() +
1103 obd->obd_recovery_timeout;
1107 /* Only start it the first time called */
1108 void target_start_recovery_timer(struct obd_device *obd, svc_handler_t handler,
1109 struct ptlrpc_request *req)
1111 spin_lock_bh(&obd->obd_processing_task_lock);
1112 if (obd->obd_recovery_handler) {
1113 spin_unlock_bh(&obd->obd_processing_task_lock);
1116 CWARN("%s: starting recovery timer\n", obd->obd_name);
1117 obd->obd_recovery_handler = handler;
1118 cfs_timer_init(&obd->obd_recovery_timer, target_recovery_expired, obd);
1119 spin_unlock_bh(&obd->obd_processing_task_lock);
1121 reset_recovery_timer(obd, req, 1);
1124 static int check_for_next_transno(struct obd_device *obd)
1126 struct ptlrpc_request *req;
1127 int wake_up = 0, connected, completed, queue_len, max;
1128 __u64 next_transno, req_transno;
1130 spin_lock_bh(&obd->obd_processing_task_lock);
1131 req = list_entry(obd->obd_recovery_queue.next,
1132 struct ptlrpc_request, rq_list);
1133 max = obd->obd_max_recoverable_clients;
1134 req_transno = lustre_msg_get_transno(req->rq_reqmsg);
1135 connected = obd->obd_connected_clients;
1136 completed = max - obd->obd_recoverable_clients;
1137 queue_len = obd->obd_requests_queued_for_recovery;
1138 next_transno = obd->obd_next_recovery_transno;
1140 CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
1141 "req_transno: "LPU64", next_transno: "LPU64"\n",
1142 max, connected, completed, queue_len, req_transno, next_transno);
1143 if (obd->obd_abort_recovery) {
1144 CDEBUG(D_HA, "waking for aborted recovery\n");
1146 } else if (!obd->obd_recovering) {
1147 CDEBUG(D_HA, "waking for completed recovery (?)\n");
1149 } else if (req_transno == next_transno) {
1150 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1152 } else if (queue_len + completed == max) {
1154 "waking for skipped transno (skip: "LPD64
1155 ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
1156 next_transno, queue_len, completed, max, req_transno);
1157 obd->obd_next_recovery_transno = req_transno;
1160 spin_unlock_bh(&obd->obd_processing_task_lock);
1161 LASSERT(lustre_msg_get_transno(req->rq_reqmsg) >= next_transno);
1165 static void process_recovery_queue(struct obd_device *obd)
1167 struct ptlrpc_request *req;
1168 int abort_recovery = 0;
1169 struct l_wait_info lwi = { 0 };
1173 spin_lock_bh(&obd->obd_processing_task_lock);
1174 LASSERT(obd->obd_processing_task == cfs_curproc_pid());
1175 req = list_entry(obd->obd_recovery_queue.next,
1176 struct ptlrpc_request, rq_list);
1178 if (lustre_msg_get_transno(req->rq_reqmsg) !=
1179 obd->obd_next_recovery_transno) {
1180 spin_unlock_bh(&obd->obd_processing_task_lock);
1181 CDEBUG(D_HA, "Waiting for transno "LPD64" (1st is "
1183 obd->obd_next_recovery_transno,
1184 lustre_msg_get_transno(req->rq_reqmsg));
1185 l_wait_event(obd->obd_next_transno_waitq,
1186 check_for_next_transno(obd), &lwi);
1187 spin_lock_bh(&obd->obd_processing_task_lock);
1188 abort_recovery = obd->obd_abort_recovery;
1189 spin_unlock_bh(&obd->obd_processing_task_lock);
1190 if (abort_recovery) {
1191 target_abort_recovery(obd);
1196 list_del_init(&req->rq_list);
1197 obd->obd_requests_queued_for_recovery--;
1198 spin_unlock_bh(&obd->obd_processing_task_lock);
1200 DEBUG_REQ(D_HA, req, "processing: ");
1201 (void)obd->obd_recovery_handler(req);
1202 obd->obd_replayed_requests++;
1203 reset_recovery_timer(obd, req, 0);
1204 /* bug 1580: decide how to properly sync() in recovery */
1205 //mds_fsync_super(obd->u.obt.obt_sb);
1206 class_export_put(req->rq_export);
1207 ptlrpc_req_drop_rs(req);
1208 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
1209 OBD_FREE(req, sizeof *req);
1210 spin_lock_bh(&obd->obd_processing_task_lock);
1211 obd->obd_next_recovery_transno++;
1212 if (list_empty(&obd->obd_recovery_queue)) {
1213 obd->obd_processing_task = 0;
1214 spin_unlock_bh(&obd->obd_processing_task_lock);
1217 spin_unlock_bh(&obd->obd_processing_task_lock);
1222 int target_queue_recovery_request(struct ptlrpc_request *req,
1223 struct obd_device *obd)
1225 struct list_head *tmp;
1227 __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
1228 struct ptlrpc_request *saved_req;
1229 struct lustre_msg *reqmsg;
1231 /* CAVEAT EMPTOR: The incoming request message has been swabbed
1232 * (i.e. buflens etc are in my own byte order), but type-dependent
1233 * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1236 CFS_INIT_LIST_HEAD(&req->rq_list);
1237 DEBUG_REQ(D_HA, req, "not queueing");
1241 /* XXX If I were a real man, these LBUGs would be sane cleanups. */
1242 /* XXX just like the request-dup code in queue_final_reply */
1243 OBD_ALLOC(saved_req, sizeof *saved_req);
1246 OBD_ALLOC(reqmsg, req->rq_reqlen);
1250 spin_lock_bh(&obd->obd_processing_task_lock);
1252 /* If we're processing the queue, we want don't want to queue this
1255 * Also, if this request has a transno less than the one we're waiting
1256 * for, we should process it now. It could (and currently always will)
1257 * be an open request for a descriptor that was opened some time ago.
1259 * Also, a resent, replayed request that has already been
1260 * handled will pass through here and be processed immediately.
1262 if (obd->obd_processing_task == cfs_curproc_pid() ||
1263 transno < obd->obd_next_recovery_transno) {
1264 /* Processing the queue right now, don't re-add. */
1265 LASSERT(list_empty(&req->rq_list));
1266 spin_unlock_bh(&obd->obd_processing_task_lock);
1267 OBD_FREE(reqmsg, req->rq_reqlen);
1268 OBD_FREE(saved_req, sizeof *saved_req);
1272 /* A resent, replayed request that is still on the queue; just drop it.
1273 The queued request will handle this. */
1274 if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT|MSG_REPLAY)) ==
1275 (MSG_RESENT | MSG_REPLAY)) {
1276 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1277 spin_unlock_bh(&obd->obd_processing_task_lock);
1278 OBD_FREE(reqmsg, req->rq_reqlen);
1279 OBD_FREE(saved_req, sizeof *saved_req);
1283 memcpy(saved_req, req, sizeof *req);
1284 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1286 req->rq_reqmsg = reqmsg;
1287 class_export_get(req->rq_export);
1288 CFS_INIT_LIST_HEAD(&req->rq_list);
1291 list_for_each(tmp, &obd->obd_recovery_queue) {
1292 struct ptlrpc_request *reqiter =
1293 list_entry(tmp, struct ptlrpc_request, rq_list);
1295 if (lustre_msg_get_transno(reqiter->rq_reqmsg) > transno) {
1296 list_add_tail(&req->rq_list, &reqiter->rq_list);
1303 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
1306 obd->obd_requests_queued_for_recovery++;
1308 if (obd->obd_processing_task != 0) {
1309 /* Someone else is processing this queue, we'll leave it to
1312 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1313 spin_unlock_bh(&obd->obd_processing_task_lock);
1317 /* Nobody is processing, and we know there's (at least) one to process
1318 * now, so we'll do the honours.
1320 obd->obd_processing_task = cfs_curproc_pid();
1321 spin_unlock_bh(&obd->obd_processing_task_lock);
1323 process_recovery_queue(obd);
1327 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1329 return req->rq_export->exp_obd;
1332 int target_queue_last_replay_reply(struct ptlrpc_request *req, int rc)
1334 struct obd_device *obd = target_req2obd(req);
1335 struct ptlrpc_request *saved_req;
1336 struct lustre_msg *reqmsg;
1337 int recovery_done = 0;
1339 LASSERT ((rc == 0) == req->rq_packed_final);
1341 if (!req->rq_packed_final) {
1342 /* Just like ptlrpc_error, but without the sending. */
1343 rc = lustre_pack_reply(req, 1, NULL, NULL);
1346 req->rq_type = PTL_RPC_MSG_ERR;
1349 LASSERT(!req->rq_reply_state->rs_difficult);
1350 LASSERT(list_empty(&req->rq_list));
1351 /* XXX a bit like the request-dup code in queue_recovery_request */
1352 OBD_ALLOC(saved_req, sizeof *saved_req);
1355 OBD_ALLOC(reqmsg, req->rq_reqlen);
1357 OBD_FREE(saved_req, sizeof *req);
1361 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1363 /* Don't race cleanup */
1364 spin_lock_bh(&obd->obd_processing_task_lock);
1365 if (obd->obd_stopping) {
1366 spin_unlock_bh(&obd->obd_processing_task_lock);
1367 OBD_FREE(reqmsg, req->rq_reqlen);
1368 OBD_FREE(saved_req, sizeof *req);
1369 req->rq_status = -ENOTCONN;
1370 /* rv is ignored anyhow */
1373 ptlrpc_rs_addref(req->rq_reply_state); /* +1 ref for saved reply */
1375 req->rq_reqmsg = reqmsg;
1376 class_export_get(req->rq_export);
1377 list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
1379 /* only count the first "replay over" request from each
1381 if (req->rq_export->exp_replay_needed) {
1382 --obd->obd_recoverable_clients;
1384 spin_lock(&req->rq_export->exp_lock);
1385 req->rq_export->exp_replay_needed = 0;
1386 spin_unlock(&req->rq_export->exp_lock);
1388 recovery_done = (obd->obd_recoverable_clients == 0);
1389 spin_unlock_bh(&obd->obd_processing_task_lock);
1391 OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
1392 if (recovery_done) {
1393 spin_lock_bh(&obd->obd_processing_task_lock);
1394 obd->obd_recovering = obd->obd_abort_recovery = 0;
1395 target_cancel_recovery_timer(obd);
1396 spin_unlock_bh(&obd->obd_processing_task_lock);
1398 target_finish_recovery(obd);
1399 CDEBUG(D_HA, "%s: recovery complete\n",
1400 obd_uuid2str(&obd->obd_uuid));
1402 CWARN("%s: %d recoverable clients remain\n",
1403 obd->obd_name, obd->obd_recoverable_clients);
1404 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1410 static inline struct ldlm_pool *ldlm_exp2pl(struct obd_export *exp)
1412 LASSERT(exp != NULL);
1413 return &exp->exp_obd->obd_namespace->ns_pool;
1416 int target_pack_pool_reply(struct ptlrpc_request *req)
1418 struct ldlm_pool *pl;
1421 if (!exp_connect_lru_resize(req->rq_export))
1424 pl = ldlm_exp2pl(req->rq_export);
1426 spin_lock(&pl->pl_lock);
1427 lustre_msg_set_slv(req->rq_repmsg, ldlm_pool_get_slv(pl));
1428 lustre_msg_set_limit(req->rq_repmsg, ldlm_pool_get_limit(pl));
1429 spin_unlock(&pl->pl_lock);
1435 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1437 if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1438 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1439 DEBUG_REQ(D_ERROR, req, "dropping reply");
1444 DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
1445 req->rq_status = rc;
1446 return (ptlrpc_error(req));
1448 DEBUG_REQ(D_NET, req, "sending reply");
1451 target_pack_pool_reply(req);
1452 return (ptlrpc_send_reply(req, PTLRPC_REPLY_MAYBE_DIFFICULT));
1456 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1459 struct ptlrpc_reply_state *rs;
1460 struct obd_device *obd;
1461 struct obd_export *exp;
1462 struct ptlrpc_service *svc;
1464 svc = req->rq_rqbd->rqbd_service;
1465 rs = req->rq_reply_state;
1466 if (rs == NULL || !rs->rs_difficult) {
1468 target_send_reply_msg (req, rc, fail_id);
1472 /* must be an export if locks saved */
1473 LASSERT (req->rq_export != NULL);
1474 /* req/reply consistent */
1475 LASSERT (rs->rs_service == svc);
1478 LASSERT (!rs->rs_scheduled);
1479 LASSERT (!rs->rs_scheduled_ever);
1480 LASSERT (!rs->rs_handled);
1481 LASSERT (!rs->rs_on_net);
1482 LASSERT (rs->rs_export == NULL);
1483 LASSERT (list_empty(&rs->rs_obd_list));
1484 LASSERT (list_empty(&rs->rs_exp_list));
1486 exp = class_export_get (req->rq_export);
1489 /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1490 rs->rs_scheduled = 1;
1492 rs->rs_xid = req->rq_xid;
1493 rs->rs_transno = req->rq_transno;
1494 rs->rs_export = exp;
1496 spin_lock(&obd->obd_uncommitted_replies_lock);
1498 if (rs->rs_transno > obd->obd_last_committed) {
1499 /* not committed already */
1500 list_add_tail (&rs->rs_obd_list,
1501 &obd->obd_uncommitted_replies);
1504 spin_unlock (&obd->obd_uncommitted_replies_lock);
1505 spin_lock (&exp->exp_lock);
1507 list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1509 spin_unlock(&exp->exp_lock);
1511 netrc = target_send_reply_msg (req, rc, fail_id);
1513 spin_lock(&svc->srv_lock);
1515 svc->srv_n_difficult_replies++;
1518 /* error sending: reply is off the net. Also we need +1
1519 * reply ref until ptlrpc_server_handle_reply() is done
1520 * with the reply state (if the send was successful, there
1521 * would have been +1 ref for the net, which
1522 * reply_out_callback leaves alone) */
1524 ptlrpc_rs_addref(rs);
1525 atomic_inc (&svc->srv_outstanding_replies);
1528 if (!rs->rs_on_net || /* some notifier */
1529 list_empty(&rs->rs_exp_list) || /* completed already */
1530 list_empty(&rs->rs_obd_list)) {
1531 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1532 cfs_waitq_signal (&svc->srv_waitq);
1534 list_add (&rs->rs_list, &svc->srv_active_replies);
1535 rs->rs_scheduled = 0; /* allow notifier to schedule */
1538 spin_unlock(&svc->srv_lock);
1541 int target_handle_ping(struct ptlrpc_request *req)
1543 obd_ping(req->rq_export);
1544 return lustre_pack_reply(req, 1, NULL, NULL);
1547 void target_committed_to_req(struct ptlrpc_request *req)
1549 struct obd_device *obd = req->rq_export->exp_obd;
1551 if (!obd->obd_no_transno && req->rq_repmsg != NULL)
1552 lustre_msg_set_last_committed(req->rq_repmsg,
1553 obd->obd_last_committed);
1555 DEBUG_REQ(D_IOCTL, req, "not sending last_committed update");
1557 CDEBUG(D_INFO, "last_committed "LPU64", xid "LPU64"\n",
1558 obd->obd_last_committed, req->rq_xid);
1561 EXPORT_SYMBOL(target_committed_to_req);
1563 #ifdef HAVE_QUOTA_SUPPORT
1564 int target_handle_qc_callback(struct ptlrpc_request *req)
1566 struct obd_quotactl *oqctl;
1567 struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
1569 oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1570 lustre_swab_obd_quotactl);
1571 if (oqctl == NULL) {
1572 CERROR("Can't unpack obd_quotactl\n");
1576 cli->cl_qchk_stat = oqctl->qc_stat;
1581 int target_handle_dqacq_callback(struct ptlrpc_request *req)
1584 struct obd_device *obd = req->rq_export->exp_obd;
1585 struct obd_device *master_obd;
1586 struct lustre_quota_ctxt *qctxt;
1587 struct qunit_data *qdata;
1589 struct qunit_data_old *qdata_old;
1591 int repsize[2] = { sizeof(struct ptlrpc_body),
1592 sizeof(struct qunit_data) };
1595 rc = lustre_pack_reply(req, 2, repsize, NULL);
1597 CERROR("packing reply failed!: rc = %d\n", rc);
1600 LASSERT(req->rq_export);
1602 /* fixed for bug10707 */
1603 if ((req->rq_export->exp_connect_flags & OBD_CONNECT_QUOTA64) &&
1604 !OBD_FAIL_CHECK(OBD_FAIL_QUOTA_QD_COUNT_32BIT)) {
1605 CDEBUG(D_QUOTA, "qd_count is 64bit!\n");
1606 rep = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1607 sizeof(struct qunit_data));
1609 qdata = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*qdata),
1612 CDEBUG(D_QUOTA, "qd_count is 32bit!\n");
1613 rep = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1614 sizeof(struct qunit_data_old));
1616 qdata_old = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*qdata_old),
1617 lustre_swab_qdata_old);
1618 qdata = lustre_quota_old_to_new(qdata_old);
1621 if (qdata == NULL) {
1622 CERROR("Can't unpack qunit_data\n");
1626 /* we use the observer */
1627 LASSERT(obd->obd_observer && obd->obd_observer->obd_observer);
1628 master_obd = obd->obd_observer->obd_observer;
1629 qctxt = &master_obd->u.obt.obt_qctxt;
1631 LASSERT(qctxt->lqc_handler);
1632 rc = qctxt->lqc_handler(master_obd, qdata,
1633 lustre_msg_get_opc(req->rq_reqmsg));
1634 if (rc && rc != -EDQUOT)
1635 CDEBUG(rc == -EBUSY ? D_QUOTA : D_ERROR,
1636 "dqacq failed! (rc:%d)\n", rc);
1638 /* the qd_count might be changed in lqc_handler */
1639 if ((req->rq_export->exp_connect_flags & OBD_CONNECT_QUOTA64) &&
1640 !OBD_FAIL_CHECK(OBD_FAIL_QUOTA_QD_COUNT_32BIT)) {
1641 memcpy(rep,qdata,sizeof(*qdata));
1643 qdata_old = lustre_quota_new_to_old(qdata);
1644 memcpy(rep,qdata_old,sizeof(*qdata_old));
1646 req->rq_status = rc;
1647 rc = ptlrpc_reply(req);
1652 #endif /* !__KERNEL__ */
1654 #endif /* HAVE_QUOTA_SUPPORT */
1656 ldlm_mode_t lck_compat_array[] = {
1657 [LCK_EX] LCK_COMPAT_EX,
1658 [LCK_PW] LCK_COMPAT_PW,
1659 [LCK_PR] LCK_COMPAT_PR,
1660 [LCK_CW] LCK_COMPAT_CW,
1661 [LCK_CR] LCK_COMPAT_CR,
1662 [LCK_NL] LCK_COMPAT_NL,
1663 [LCK_GROUP] LCK_COMPAT_GROUP