1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 # define EXPORT_SYMTAB
40 #define DEBUG_SUBSYSTEM S_LDLM
43 # include <libcfs/libcfs.h>
45 # include <liblustre.h>
48 #include <lustre_mds.h>
49 #include <lustre_dlm.h>
50 #include <lustre_net.h>
51 #include <lustre_sec.h>
52 #include "ldlm_internal.h"
55 /* @priority: if non-zero, move the selected to the list head
56 * @create: if zero, only search in existed connections
58 static int import_set_conn(struct obd_import *imp, struct obd_uuid *uuid,
59 int priority, int create)
61 struct ptlrpc_connection *ptlrpc_conn;
62 struct obd_import_conn *imp_conn = NULL, *item;
66 if (!create && !priority) {
67 CDEBUG(D_HA, "Nothing to do\n");
71 ptlrpc_conn = ptlrpc_uuid_to_connection(uuid);
73 CDEBUG(D_HA, "can't find connection %s\n", uuid->uuid);
78 OBD_ALLOC(imp_conn, sizeof(*imp_conn));
80 GOTO(out_put, rc = -ENOMEM);
84 spin_lock(&imp->imp_lock);
85 list_for_each_entry(item, &imp->imp_conn_list, oic_item) {
86 if (obd_uuid_equals(uuid, &item->oic_uuid)) {
88 list_del(&item->oic_item);
89 list_add(&item->oic_item, &imp->imp_conn_list);
90 item->oic_last_attempt = 0;
92 CDEBUG(D_HA, "imp %p@%s: found existing conn %s%s\n",
93 imp, imp->imp_obd->obd_name, uuid->uuid,
94 (priority ? ", moved to head" : ""));
95 spin_unlock(&imp->imp_lock);
96 GOTO(out_free, rc = 0);
101 imp_conn->oic_conn = ptlrpc_conn;
102 imp_conn->oic_uuid = *uuid;
103 item->oic_last_attempt = 0;
105 list_add(&imp_conn->oic_item, &imp->imp_conn_list);
107 list_add_tail(&imp_conn->oic_item, &imp->imp_conn_list);
108 CDEBUG(D_HA, "imp %p@%s: add connection %s at %s\n",
109 imp, imp->imp_obd->obd_name, uuid->uuid,
110 (priority ? "head" : "tail"));
112 spin_unlock(&imp->imp_lock);
113 GOTO(out_free, rc = -ENOENT);
116 spin_unlock(&imp->imp_lock);
120 OBD_FREE(imp_conn, sizeof(*imp_conn));
122 ptlrpc_connection_put(ptlrpc_conn);
126 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid)
128 return import_set_conn(imp, uuid, 1, 0);
131 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
134 return import_set_conn(imp, uuid, priority, 1);
137 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
139 struct obd_import_conn *imp_conn;
140 struct obd_export *dlmexp;
144 spin_lock(&imp->imp_lock);
145 if (list_empty(&imp->imp_conn_list)) {
146 LASSERT(!imp->imp_connection);
150 list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
151 if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
153 LASSERT(imp_conn->oic_conn);
155 /* is current conn? */
156 if (imp_conn == imp->imp_conn_current) {
157 LASSERT(imp_conn->oic_conn == imp->imp_connection);
159 if (imp->imp_state != LUSTRE_IMP_CLOSED &&
160 imp->imp_state != LUSTRE_IMP_DISCON) {
161 CERROR("can't remove current connection\n");
162 GOTO(out, rc = -EBUSY);
165 ptlrpc_connection_put(imp->imp_connection);
166 imp->imp_connection = NULL;
168 dlmexp = class_conn2export(&imp->imp_dlm_handle);
169 if (dlmexp && dlmexp->exp_connection) {
170 LASSERT(dlmexp->exp_connection ==
172 ptlrpc_connection_put(dlmexp->exp_connection);
173 dlmexp->exp_connection = NULL;
177 list_del(&imp_conn->oic_item);
178 ptlrpc_connection_put(imp_conn->oic_conn);
179 OBD_FREE(imp_conn, sizeof(*imp_conn));
180 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
181 imp, imp->imp_obd->obd_name, uuid->uuid);
186 spin_unlock(&imp->imp_lock);
188 CERROR("connection %s not found\n", uuid->uuid);
192 static void destroy_import(struct obd_import *imp)
194 /* drop security policy instance after all rpc finished/aborted
195 * to let all busy contexts be released. */
196 class_import_get(imp);
197 class_destroy_import(imp);
198 sptlrpc_import_sec_put(imp);
199 class_import_put(imp);
202 /* configure an RPC client OBD device
207 * 3 - inactive-on-startup
209 int client_obd_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
211 struct client_obd *cli = &obddev->u.cli;
212 struct obd_import *imp;
213 struct obd_uuid server_uuid;
214 int rq_portal, rp_portal, connect_op;
215 char *name = obddev->obd_type->typ_name;
219 /* In a more perfect world, we would hang a ptlrpc_client off of
220 * obd_type and just use the values from there. */
221 if (!strcmp(name, LUSTRE_OSC_NAME)) {
222 rq_portal = OST_REQUEST_PORTAL;
223 rp_portal = OSC_REPLY_PORTAL;
224 connect_op = OST_CONNECT;
225 } else if (!strcmp(name, LUSTRE_MDC_NAME)) {
226 rq_portal = MDS_REQUEST_PORTAL;
227 rp_portal = MDC_REPLY_PORTAL;
228 connect_op = MDS_CONNECT;
229 } else if (!strcmp(name, LUSTRE_MGC_NAME)) {
230 rq_portal = MGS_REQUEST_PORTAL;
231 rp_portal = MGC_REPLY_PORTAL;
232 connect_op = MGS_CONNECT;
234 CERROR("unknown client OBD type \"%s\", can't setup\n",
239 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
240 CERROR("requires a TARGET UUID\n");
244 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
245 CERROR("client UUID must be less than 38 characters\n");
249 if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
250 CERROR("setup requires a SERVER UUID\n");
254 if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
255 CERROR("target UUID must be less than 38 characters\n");
259 init_rwsem(&cli->cl_sem);
260 sema_init(&cli->cl_mgc_sem, 1);
261 sptlrpc_rule_set_init(&cli->cl_sptlrpc_rset);
262 cli->cl_sec_part = LUSTRE_SP_ANY;
263 cli->cl_conn_count = 0;
264 memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
265 min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
266 sizeof(server_uuid)));
269 cli->cl_avail_grant = 0;
270 /* FIXME: should limit this for the sum of all cl_dirty_max */
271 cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
272 if (cli->cl_dirty_max >> CFS_PAGE_SHIFT > num_physpages / 8)
273 cli->cl_dirty_max = num_physpages << (CFS_PAGE_SHIFT - 3);
274 CFS_INIT_LIST_HEAD(&cli->cl_cache_waiters);
275 CFS_INIT_LIST_HEAD(&cli->cl_loi_ready_list);
276 CFS_INIT_LIST_HEAD(&cli->cl_loi_write_list);
277 CFS_INIT_LIST_HEAD(&cli->cl_loi_read_list);
278 client_obd_list_lock_init(&cli->cl_loi_list_lock);
279 cli->cl_r_in_flight = 0;
280 cli->cl_w_in_flight = 0;
282 spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
283 spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
284 spin_lock_init(&cli->cl_read_page_hist.oh_lock);
285 spin_lock_init(&cli->cl_write_page_hist.oh_lock);
286 spin_lock_init(&cli->cl_read_offset_hist.oh_lock);
287 spin_lock_init(&cli->cl_write_offset_hist.oh_lock);
288 cfs_waitq_init(&cli->cl_destroy_waitq);
289 atomic_set(&cli->cl_destroy_in_flight, 0);
290 #ifdef ENABLE_CHECKSUM
291 /* Turn on checksumming by default. */
292 cli->cl_checksum = 1;
294 * The supported checksum types will be worked out at connect time
295 * Set cl_chksum* to CRC32 for now to avoid returning screwed info
298 cli->cl_cksum_type = cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
300 atomic_set(&cli->cl_resends, OSC_DEFAULT_RESENDS);
302 /* This value may be changed at connect time in
303 ptlrpc_connect_interpret. */
304 cli->cl_max_pages_per_rpc = min((int)PTLRPC_MAX_BRW_PAGES,
305 (int)(1024 * 1024 >> CFS_PAGE_SHIFT));
307 if (!strcmp(name, LUSTRE_MDC_NAME)) {
308 cli->cl_max_rpcs_in_flight = MDC_MAX_RIF_DEFAULT;
309 } else if (num_physpages >> (20 - CFS_PAGE_SHIFT) <= 128 /* MB */) {
310 cli->cl_max_rpcs_in_flight = 2;
311 } else if (num_physpages >> (20 - CFS_PAGE_SHIFT) <= 256 /* MB */) {
312 cli->cl_max_rpcs_in_flight = 3;
313 } else if (num_physpages >> (20 - CFS_PAGE_SHIFT) <= 512 /* MB */) {
314 cli->cl_max_rpcs_in_flight = 4;
316 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
321 CERROR("ldlm_get_ref failed: %d\n", rc);
325 ptlrpc_init_client(rq_portal, rp_portal, name,
326 &obddev->obd_ldlm_client);
328 imp = class_new_import(obddev);
330 GOTO(err_ldlm, rc = -ENOENT);
331 imp->imp_client = &obddev->obd_ldlm_client;
332 imp->imp_connect_op = connect_op;
333 imp->imp_initial_recov = 1;
334 imp->imp_initial_recov_bk = 0;
335 CFS_INIT_LIST_HEAD(&imp->imp_pinger_chain);
336 memcpy(cli->cl_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
337 LUSTRE_CFG_BUFLEN(lcfg, 1));
338 class_import_put(imp);
340 rc = client_import_add_conn(imp, &server_uuid, 1);
342 CERROR("can't add initial connection\n");
343 GOTO(err_import, rc);
346 cli->cl_import = imp;
347 /* cli->cl_max_mds_{easize,cookiesize} updated by mdc_init_ea_size() */
348 cli->cl_max_mds_easize = sizeof(struct lov_mds_md_v3);
349 cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
351 if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
352 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
353 CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
354 name, obddev->obd_name,
355 cli->cl_target_uuid.uuid);
356 spin_lock(&imp->imp_lock);
357 imp->imp_invalid = 1;
358 spin_unlock(&imp->imp_lock);
362 cli->cl_qchk_stat = CL_NOT_QUOTACHECKED;
367 class_destroy_import(imp);
375 int client_obd_cleanup(struct obd_device *obddev)
378 sptlrpc_rule_set_free(&obddev->u.cli.cl_sptlrpc_rset);
383 /* ->o_connect() method for client side (OSC and MDC and MGC) */
384 int client_connect_import(const struct lu_env *env,
385 struct lustre_handle *dlm_handle,
386 struct obd_device *obd, struct obd_uuid *cluuid,
387 struct obd_connect_data *data, void *localdata)
389 struct client_obd *cli = &obd->u.cli;
390 struct obd_import *imp = cli->cl_import;
391 struct obd_export *exp;
392 struct obd_connect_data *ocd;
393 struct ldlm_namespace *to_be_freed = NULL;
397 down_write(&cli->cl_sem);
398 rc = class_connect(dlm_handle, obd, cluuid);
402 cli->cl_conn_count++;
403 if (cli->cl_conn_count > 1)
405 exp = class_conn2export(dlm_handle);
407 if (obd->obd_namespace != NULL)
408 CERROR("already have namespace!\n");
409 obd->obd_namespace = ldlm_namespace_new(obd, obd->obd_name,
410 LDLM_NAMESPACE_CLIENT,
411 LDLM_NAMESPACE_GREEDY);
412 if (obd->obd_namespace == NULL)
413 GOTO(out_disco, rc = -ENOMEM);
415 imp->imp_dlm_handle = *dlm_handle;
416 rc = ptlrpc_init_import(imp);
420 ocd = &imp->imp_connect_data;
423 imp->imp_connect_flags_orig = data->ocd_connect_flags;
426 rc = ptlrpc_connect_import(imp, NULL);
428 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
431 LASSERT(exp->exp_connection);
434 LASSERTF((ocd->ocd_connect_flags & data->ocd_connect_flags) ==
435 ocd->ocd_connect_flags, "old "LPX64", new "LPX64"\n",
436 data->ocd_connect_flags, ocd->ocd_connect_flags);
437 data->ocd_connect_flags = ocd->ocd_connect_flags;
440 ptlrpc_pinger_add_import(imp);
446 ldlm_namespace_free_prior(obd->obd_namespace, imp, 0);
447 to_be_freed = obd->obd_namespace;
448 obd->obd_namespace = NULL;
450 cli->cl_conn_count--;
451 class_disconnect(exp);
453 class_export_put(exp);
456 up_write(&cli->cl_sem);
458 ldlm_namespace_free_post(to_be_freed);
463 int client_disconnect_export(struct obd_export *exp)
465 struct obd_device *obd = class_exp2obd(exp);
466 struct client_obd *cli;
467 struct obd_import *imp;
469 struct ldlm_namespace *to_be_freed = NULL;
473 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
474 exp, exp ? exp->exp_handle.h_cookie : -1);
479 imp = cli->cl_import;
481 down_write(&cli->cl_sem);
482 if (!cli->cl_conn_count) {
483 CERROR("disconnecting disconnected device (%s)\n",
485 GOTO(out_sem, rc = -EINVAL);
488 cli->cl_conn_count--;
489 if (cli->cl_conn_count)
490 GOTO(out_no_disconnect, rc = 0);
492 /* Mark import deactivated now, so we don't try to reconnect if any
493 * of the cleanup RPCs fails (e.g. ldlm cancel, etc). We don't
494 * fully deactivate the import, or that would drop all requests. */
495 spin_lock(&imp->imp_lock);
496 imp->imp_deactive = 1;
497 spin_unlock(&imp->imp_lock);
499 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
500 * delete it regardless. (It's safe to delete an import that was
502 (void)ptlrpc_pinger_del_import(imp);
504 if (obd->obd_namespace != NULL) {
505 /* obd_force == local only */
506 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
507 obd->obd_force ? LDLM_FL_LOCAL_ONLY:0,
509 ldlm_namespace_free_prior(obd->obd_namespace, imp, obd->obd_force);
510 to_be_freed = obd->obd_namespace;
513 rc = ptlrpc_disconnect_import(imp, 0);
515 ptlrpc_invalidate_import(imp);
516 /* set obd_namespace to NULL only after invalidate, because we can have
517 * some connect requests in flight, and his need store a connect flags
518 * in obd_namespace. bug 14260 */
519 obd->obd_namespace = NULL;
521 if (imp->imp_rq_pool) {
522 ptlrpc_free_rq_pool(imp->imp_rq_pool);
523 imp->imp_rq_pool = NULL;
526 cli->cl_import = NULL;
530 err = class_disconnect(exp);
534 up_write(&cli->cl_sem);
536 ldlm_namespace_free_post(to_be_freed);
541 /* --------------------------------------------------------------------------
542 * from old lib/target.c
543 * -------------------------------------------------------------------------- */
545 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
546 struct obd_uuid *cluuid, int initial_conn)
549 if (exp->exp_connection && exp->exp_imp_reverse && !initial_conn) {
550 struct lustre_handle *hdl;
551 hdl = &exp->exp_imp_reverse->imp_remote_handle;
552 /* Might be a re-connect after a partition. */
553 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
554 CWARN("%s: %s reconnecting\n", exp->exp_obd->obd_name,
556 conn->cookie = exp->exp_handle.h_cookie;
557 /* target_handle_connect() treats EALREADY and
558 * -EALREADY differently. EALREADY means we are
559 * doing a valid reconnect from the same client. */
562 CERROR("%s reconnecting from %s, "
563 "handle mismatch (ours "LPX64", theirs "
564 LPX64")\n", cluuid->uuid,
565 exp->exp_connection->c_remote_uuid.uuid,
566 hdl->cookie, conn->cookie);
567 memset(conn, 0, sizeof *conn);
568 /* target_handle_connect() treats EALREADY and
569 * -EALREADY differently. -EALREADY is an error
570 * (same UUID, different handle). */
575 conn->cookie = exp->exp_handle.h_cookie;
576 CDEBUG(D_HA, "connect export for UUID '%s' at %p, cookie "LPX64"\n",
577 cluuid->uuid, exp, conn->cookie);
581 void target_client_add_cb(struct obd_device *obd, __u64 transno, void *cb_data,
584 struct obd_export *exp = cb_data;
586 CDEBUG(D_RPCTRACE, "%s: committing for initial connect of %s\n",
587 obd->obd_name, exp->exp_client_uuid.uuid);
589 spin_lock(&exp->exp_lock);
590 exp->exp_need_sync = 0;
591 spin_unlock(&exp->exp_lock);
593 EXPORT_SYMBOL(target_client_add_cb);
595 target_start_and_reset_recovery_timer(struct obd_device *obd,
596 struct ptlrpc_request *req,
599 int target_handle_connect(struct ptlrpc_request *req)
601 struct obd_device *target, *targref = NULL;
602 struct obd_export *export = NULL;
603 struct obd_import *revimp;
604 struct lustre_handle conn;
605 struct lustre_handle *tmp;
606 struct obd_uuid tgtuuid;
607 struct obd_uuid cluuid;
608 struct obd_uuid remote_uuid;
611 int initial_conn = 0;
612 struct obd_connect_data *data, *tmpdata;
613 lnet_nid_t *client_nid = NULL;
616 OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
618 str = req_capsule_client_get(&req->rq_pill, &RMF_TGTUUID);
620 DEBUG_REQ(D_ERROR, req, "bad target UUID for connect");
621 GOTO(out, rc = -EINVAL);
624 obd_str2uuid(&tgtuuid, str);
625 target = class_uuid2obd(&tgtuuid);
627 target = class_name2obd(str);
629 if (!target || target->obd_stopping || !target->obd_set_up) {
630 LCONSOLE_ERROR_MSG(0x137, "UUID '%s' is not available "
631 " for connect (%s)\n", str,
632 !target ? "no target" :
633 (target->obd_stopping ? "stopping" :
635 GOTO(out, rc = -ENODEV);
638 if (target->obd_no_conn) {
639 LCONSOLE_WARN("%s: temporarily refusing client connection "
640 "from %s\n", target->obd_name,
641 libcfs_nid2str(req->rq_peer.nid));
642 GOTO(out, rc = -EAGAIN);
645 /* Make sure the target isn't cleaned up while we're here. Yes,
646 there's still a race between the above check and our incref here.
647 Really, class_uuid2obd should take the ref. */
648 targref = class_incref(target, __FUNCTION__, cfs_current());
651 str = req_capsule_client_get(&req->rq_pill, &RMF_CLUUID);
653 DEBUG_REQ(D_ERROR, req, "bad client UUID for connect");
654 GOTO(out, rc = -EINVAL);
657 obd_str2uuid(&cluuid, str);
659 /* XXX extract a nettype and format accordingly */
660 switch (sizeof(lnet_nid_t)) {
661 /* NB the casts only avoid compiler warnings */
663 snprintf(remote_uuid.uuid, sizeof remote_uuid,
664 "NET_"LPX64"_UUID", (__u64)req->rq_peer.nid);
667 snprintf(remote_uuid.uuid, sizeof remote_uuid,
668 "NET_%x_UUID", (__u32)req->rq_peer.nid);
674 tmp = req_capsule_client_get(&req->rq_pill, &RMF_CONN);
676 GOTO(out, rc = -EPROTO);
680 data = req_capsule_client_get(&req->rq_pill, &RMF_CONNECT_DATA);
682 GOTO(out, rc = -EPROTO);
684 rc = req_capsule_server_pack(&req->rq_pill);
688 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
690 DEBUG_REQ(D_WARNING, req, "Refusing old (unversioned) "
691 "libclient connection attempt");
692 GOTO(out, rc = -EPROTO);
693 } else if (data->ocd_version < LUSTRE_VERSION_CODE -
694 LUSTRE_VERSION_ALLOWED_OFFSET ||
695 data->ocd_version > LUSTRE_VERSION_CODE +
696 LUSTRE_VERSION_ALLOWED_OFFSET) {
697 DEBUG_REQ(D_WARNING, req, "Refusing %s (%d.%d.%d.%d) "
698 "libclient connection attempt",
699 data->ocd_version < LUSTRE_VERSION_CODE ?
701 OBD_OCD_VERSION_MAJOR(data->ocd_version),
702 OBD_OCD_VERSION_MINOR(data->ocd_version),
703 OBD_OCD_VERSION_PATCH(data->ocd_version),
704 OBD_OCD_VERSION_FIX(data->ocd_version));
705 data = req_capsule_server_sized_get(&req->rq_pill,
707 offsetof(typeof(*data), ocd_version) +
708 sizeof(data->ocd_version));
710 data->ocd_connect_flags = OBD_CONNECT_VERSION;
711 data->ocd_version = LUSTRE_VERSION_CODE;
713 GOTO(out, rc = -EPROTO);
717 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_INITIAL)
720 /* lctl gets a backstage, all-access pass. */
721 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
722 goto dont_check_exports;
724 spin_lock(&target->obd_dev_lock);
725 export = lustre_hash_lookup(target->obd_uuid_hash, &cluuid);
727 if (export != NULL && export->exp_connecting) { /* bug 9635, et. al. */
728 CWARN("%s: exp %p already connecting\n",
729 export->exp_obd->obd_name, export);
730 class_export_put(export);
733 } else if (export != NULL && export->exp_connection != NULL &&
734 req->rq_peer.nid != export->exp_connection->c_peer.nid) {
735 /* make darn sure this is coming from the same peer
736 * if the UUIDs matched */
737 CWARN("%s: cookie %s seen on new NID %s when "
738 "existing NID %s is already connected\n",
739 target->obd_name, cluuid.uuid,
740 libcfs_nid2str(req->rq_peer.nid),
741 libcfs_nid2str(export->exp_connection->c_peer.nid));
742 class_export_put(export);
745 } else if (export != NULL) {
746 spin_lock(&export->exp_lock);
747 export->exp_connecting = 1;
748 spin_unlock(&export->exp_lock);
749 class_export_put(export);
750 spin_unlock(&target->obd_dev_lock);
751 LASSERT(export->exp_obd == target);
753 rc = target_handle_reconnect(&conn, export, &cluuid, initial_conn);
756 /* If we found an export, we already unlocked. */
758 spin_unlock(&target->obd_dev_lock);
759 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_CONNECT, 2 * obd_timeout);
760 } else if (req->rq_export == NULL &&
761 atomic_read(&export->exp_rpc_count) > 0) {
762 CWARN("%s: refuse connection from %s/%s to 0x%p/%d\n",
763 target->obd_name, cluuid.uuid,
764 libcfs_nid2str(req->rq_peer.nid),
765 export, atomic_read(&export->exp_refcount));
766 GOTO(out, rc = -EBUSY);
767 } else if (req->rq_export != NULL &&
768 (atomic_read(&export->exp_rpc_count) > 1)) {
769 CWARN("%s: refuse reconnection from %s@%s to 0x%p/%d\n",
770 target->obd_name, cluuid.uuid,
771 libcfs_nid2str(req->rq_peer.nid),
772 export, atomic_read(&export->exp_rpc_count));
773 GOTO(out, rc = -EBUSY);
774 } else if (lustre_msg_get_conn_cnt(req->rq_reqmsg) == 1 &&
776 CERROR("%s: NID %s (%s) reconnected with 1 conn_cnt; "
777 "cookies not random?\n", target->obd_name,
778 libcfs_nid2str(req->rq_peer.nid), cluuid.uuid);
779 GOTO(out, rc = -EALREADY);
781 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_RECONNECT, 2 * obd_timeout);
782 if (req->rq_export == NULL && initial_conn)
783 export->exp_last_request_time =
784 max(export->exp_last_request_time,
785 (time_t)cfs_time_current_sec());
792 CWARN("%s: connection from %s@%s %st"LPU64" exp %p cur %ld last %ld\n",
793 target->obd_name, cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
794 target->obd_recovering ? "recovering/" : "", data->ocd_transno,
795 export, (long)cfs_time_current_sec(),
796 export ? (long)export->exp_last_request_time : 0);
798 /* Tell the client if we're in recovery. */
799 if (target->obd_recovering) {
800 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
801 /* If this is the first time a client connects,
802 reset the recovery timer */
804 target_start_and_reset_recovery_timer(target, req,
808 /* We want to handle EALREADY but *not* -EALREADY from
809 * target_handle_reconnect(), return reconnection state in a flag */
810 if (rc == EALREADY) {
811 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
817 /* Tell the client if we support replayable requests */
818 if (target->obd_replayable)
819 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
820 client_nid = &req->rq_peer.nid;
822 if (export == NULL) {
823 if (target->obd_recovering) {
826 t = cfs_timer_deadline(&target->obd_recovery_timer);
827 t = cfs_time_sub(t, cfs_time_current());
828 CERROR("%s: denying connection for new client %s (%s): "
829 "%d clients in recovery for "CFS_TIME_T"s\n",
831 libcfs_nid2str(req->rq_peer.nid), cluuid.uuid,
832 target->obd_recoverable_clients,
833 cfs_duration_sec(t));
837 rc = obd_connect(req->rq_svc_thread->t_env,
838 &conn, target, &cluuid, data,
842 rc = obd_reconnect(req->rq_svc_thread->t_env,
843 export, target, &cluuid, data, client_nid);
847 /* Return only the parts of obd_connect_data that we understand, so the
848 * client knows that we don't understand the rest. */
850 tmpdata = req_capsule_server_get(&req->rq_pill,
852 //data->ocd_connect_flags &= OBD_CONNECT_SUPPORTED;
856 /* If all else goes well, this is our RPC return code. */
859 lustre_msg_set_handle(req->rq_repmsg, &conn);
861 /* ownership of this export ref transfers to the request AFTER we
862 * drop any previous reference the request had, but we don't want
863 * that to go to zero before we get our new export reference. */
864 export = class_conn2export(&conn);
866 DEBUG_REQ(D_ERROR, req, "Missing export!");
867 GOTO(out, rc = -ENODEV);
870 /* If the client and the server are the same node, we will already
871 * have an export that really points to the client's DLM export,
872 * because we have a shared handles table.
874 * XXX this will go away when shaver stops sending the "connect" handle
875 * in the real "remote handle" field of the request --phik 24 Apr 2003
877 if (req->rq_export != NULL)
878 class_export_put(req->rq_export);
880 req->rq_export = export;
882 spin_lock(&export->exp_lock);
884 lustre_msg_set_conn_cnt(req->rq_repmsg, export->exp_conn_cnt + 1);
885 } else if (export->exp_conn_cnt >= lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
886 spin_unlock(&export->exp_lock);
887 CERROR("%s: %s already connected at higher conn_cnt: %d > %d\n",
888 cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
889 export->exp_conn_cnt,
890 lustre_msg_get_conn_cnt(req->rq_reqmsg));
892 GOTO(out, rc = -EALREADY);
894 export->exp_conn_cnt = lustre_msg_get_conn_cnt(req->rq_reqmsg);
896 /* request from liblustre? Don't evict it for not pinging. */
897 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
898 export->exp_libclient = 1;
899 spin_unlock(&export->exp_lock);
901 spin_lock(&target->obd_dev_lock);
902 list_del_init(&export->exp_obd_chain_timed);
903 spin_unlock(&target->obd_dev_lock);
905 spin_unlock(&export->exp_lock);
908 if (export->exp_connection != NULL)
909 ptlrpc_connection_put(export->exp_connection);
910 export->exp_connection = ptlrpc_connection_get(req->rq_peer,
913 if (hlist_unhashed(&export->exp_nid_hash)) {
914 lustre_hash_add_unique(export->exp_obd->obd_nid_hash,
915 &export->exp_connection->c_peer.nid,
916 &export->exp_nid_hash);
919 spin_lock_bh(&target->obd_processing_task_lock);
920 if (target->obd_recovering && !export->exp_in_recovery) {
921 spin_lock(&export->exp_lock);
922 export->exp_in_recovery = 1;
923 export->exp_req_replay_needed = 1;
924 export->exp_lock_replay_needed = 1;
925 spin_unlock(&export->exp_lock);
926 if ((lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_TRANSNO)
927 && (data->ocd_transno == 0))
928 CWARN("Connect with zero transno!\n");
930 if ((lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_TRANSNO)
931 && data->ocd_transno < target->obd_next_recovery_transno)
932 target->obd_next_recovery_transno = data->ocd_transno;
933 target->obd_connected_clients++;
934 /* each connected client is counted as recoverable */
935 target->obd_recoverable_clients++;
936 atomic_inc(&target->obd_req_replay_clients);
937 atomic_inc(&target->obd_lock_replay_clients);
938 if (target->obd_connected_clients ==
939 target->obd_max_recoverable_clients)
940 wake_up(&target->obd_next_transno_waitq);
942 spin_unlock_bh(&target->obd_processing_task_lock);
943 tmp = req_capsule_client_get(&req->rq_pill, &RMF_CONN);
946 if (export->exp_imp_reverse != NULL) {
947 /* destroyed import can be still referenced in ctxt */
948 obd_set_info_async(export, sizeof(KEY_REVIMP_UPD),
949 KEY_REVIMP_UPD, 0, NULL, NULL);
951 /* in some recovery senarios, previous ctx init rpc handled
952 * in sptlrpc_target_export_check() might be used to install
953 * a reverse ctx in this reverse import, and later OBD_CONNECT
954 * using the same gss ctx could reach here and following new
955 * reverse import. note all reverse ctx in new/old import are
956 * actually based on the same gss ctx. so we invalidate ctx
957 * here before destroy import, otherwise flush old import will
958 * lead to remote reverse ctx be destroied, thus the reverse
959 * ctx of new import will lost its peer.
960 * there might be a better way to deal with this???
962 sptlrpc_import_inval_all_ctx(export->exp_imp_reverse);
964 destroy_import(export->exp_imp_reverse);
967 /* for the rest part, we return -ENOTCONN in case of errors
968 * in order to let client initialize connection again.
970 revimp = export->exp_imp_reverse = class_new_import(target);
972 CERROR("fail to alloc new reverse import.\n");
973 GOTO(out, rc = -ENOTCONN);
976 revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
977 revimp->imp_client = &export->exp_obd->obd_ldlm_client;
978 revimp->imp_remote_handle = conn;
979 revimp->imp_dlm_fake = 1;
980 revimp->imp_state = LUSTRE_IMP_FULL;
982 /* unknown versions will be caught in
983 * ptlrpc_handle_server_req_in->lustre_unpack_msg() */
984 revimp->imp_msg_magic = req->rq_reqmsg->lm_magic;
986 if ((export->exp_connect_flags & OBD_CONNECT_AT) &&
987 (revimp->imp_msg_magic != LUSTRE_MSG_MAGIC_V1))
988 revimp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
990 revimp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
992 rc = sptlrpc_import_sec_adapt(revimp, req->rq_svc_ctx,
993 req->rq_flvr.sf_rpc);
995 CERROR("Failed to get sec for reverse import: %d\n", rc);
996 export->exp_imp_reverse = NULL;
997 class_destroy_import(revimp);
1000 class_import_put(revimp);
1003 spin_lock(&export->exp_lock);
1004 export->exp_connecting = 0;
1005 spin_unlock(&export->exp_lock);
1008 class_decref(targref, __FUNCTION__, cfs_current());
1010 req->rq_status = rc;
1014 int target_handle_disconnect(struct ptlrpc_request *req)
1019 rc = req_capsule_server_pack(&req->rq_pill);
1023 /* keep the rq_export around so we can send the reply */
1024 req->rq_status = obd_disconnect(class_export_get(req->rq_export));
1029 void target_destroy_export(struct obd_export *exp)
1031 /* exports created from last_rcvd data, and "fake"
1032 exports created by lctl don't have an import */
1033 if (exp->exp_imp_reverse != NULL)
1034 destroy_import(exp->exp_imp_reverse);
1036 /* We cancel locks at disconnect time, but this will catch any locks
1037 * granted in a race with recovery-induced disconnect. */
1038 if (exp->exp_obd->obd_namespace != NULL)
1039 ldlm_cancel_locks_for_export(exp);
1043 * Recovery functions
1046 struct ptlrpc_request *ptlrpc_clone_req( struct ptlrpc_request *orig_req)
1048 struct ptlrpc_request *copy_req;
1049 struct lustre_msg *copy_reqmsg;
1050 struct ptlrpc_user_desc *udesc = NULL;
1052 OBD_ALLOC_PTR(copy_req);
1055 OBD_ALLOC(copy_reqmsg, orig_req->rq_reqlen);
1057 OBD_FREE_PTR(copy_req);
1061 if (orig_req->rq_user_desc) {
1062 int ngroups = orig_req->rq_user_desc->pud_ngroups;
1064 OBD_ALLOC(udesc, sptlrpc_user_desc_size(ngroups));
1066 OBD_FREE(copy_reqmsg, orig_req->rq_reqlen);
1067 OBD_FREE_PTR(copy_req);
1070 memcpy(udesc, orig_req->rq_user_desc,
1071 sptlrpc_user_desc_size(ngroups));
1074 *copy_req = *orig_req;
1075 memcpy(copy_reqmsg, orig_req->rq_reqmsg, orig_req->rq_reqlen);
1076 copy_req->rq_reqmsg = copy_reqmsg;
1077 copy_req->rq_user_desc = udesc;
1079 class_export_rpc_get(copy_req->rq_export);
1080 CFS_INIT_LIST_HEAD(©_req->rq_list);
1081 CFS_INIT_LIST_HEAD(©_req->rq_replay_list);
1082 sptlrpc_svc_ctx_addref(copy_req);
1084 if (copy_req->rq_reply_state) {
1085 /* the copied req takes over the reply state */
1086 orig_req->rq_reply_state = NULL;
1087 /* to catch further access */
1088 orig_req->rq_repmsg = NULL;
1089 orig_req->rq_replen = 0;
1095 void ptlrpc_free_clone(struct ptlrpc_request *req)
1097 LASSERT(list_empty(&req->rq_replay_list));
1099 ptlrpc_req_drop_rs(req);
1100 sptlrpc_svc_ctx_decref(req);
1101 class_export_rpc_put(req->rq_export);
1102 list_del(&req->rq_list);
1104 if (req->rq_user_desc) {
1105 int ngroups = req->rq_user_desc->pud_ngroups;
1106 OBD_FREE(req->rq_user_desc, sptlrpc_user_desc_size(ngroups));
1108 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
1112 static int target_exp_enqueue_req_replay(struct ptlrpc_request *req)
1114 __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
1115 struct obd_export *exp = req->rq_export;
1116 struct ptlrpc_request *reqiter;
1121 spin_lock(&exp->exp_lock);
1122 list_for_each_entry(reqiter, &exp->exp_req_replay_queue,
1124 if (lustre_msg_get_transno(reqiter->rq_reqmsg) == transno) {
1131 /* we expect it with RESENT and REPLAY flags */
1132 if ((lustre_msg_get_flags(req->rq_reqmsg) &
1133 (MSG_RESENT | MSG_REPLAY)) != (MSG_RESENT | MSG_REPLAY))
1134 CERROR("invalid flags %x of resent replay\n",
1135 lustre_msg_get_flags(req->rq_reqmsg));
1137 list_add_tail(&req->rq_replay_list, &exp->exp_req_replay_queue);
1140 spin_unlock(&exp->exp_lock);
1144 static void target_exp_dequeue_req_replay(struct ptlrpc_request *req)
1146 LASSERT(!list_empty(&req->rq_replay_list));
1147 LASSERT(req->rq_export);
1149 spin_lock(&req->rq_export->exp_lock);
1150 list_del_init(&req->rq_replay_list);
1151 spin_unlock(&req->rq_export->exp_lock);
1155 static void target_finish_recovery(struct obd_device *obd)
1158 LCONSOLE_INFO("%s: sending delayed replies to recovered clients\n",
1161 ldlm_reprocess_all_ns(obd->obd_namespace);
1163 /* when recovery finished, cleanup orphans on mds and ost */
1164 if (OBT(obd) && OBP(obd, postrecov)) {
1165 int rc = OBP(obd, postrecov)(obd);
1166 LCONSOLE_WARN("%s: recovery %s: rc %d\n", obd->obd_name,
1167 rc < 0 ? "failed" : "complete", rc);
1170 obd->obd_recovery_end = cfs_time_current_sec();
1174 static void abort_req_replay_queue(struct obd_device *obd)
1176 struct ptlrpc_request *req, *n;
1178 list_for_each_entry_safe(req, n, &obd->obd_req_replay_queue, rq_list) {
1179 DEBUG_REQ(D_WARNING, req, "aborted:");
1180 req->rq_status = -ENOTCONN;
1181 if (ptlrpc_error(req)) {
1182 DEBUG_REQ(D_ERROR, req,
1183 "failed abort_req_reply; skipping");
1185 target_exp_dequeue_req_replay(req);
1186 ptlrpc_free_clone(req);
1190 static void abort_lock_replay_queue(struct obd_device *obd)
1192 struct ptlrpc_request *req, *n;
1194 list_for_each_entry_safe(req, n, &obd->obd_lock_replay_queue, rq_list){
1195 DEBUG_REQ(D_ERROR, req, "aborted:");
1196 req->rq_status = -ENOTCONN;
1197 if (ptlrpc_error(req)) {
1198 DEBUG_REQ(D_ERROR, req,
1199 "failed abort_lock_reply; skipping");
1201 ptlrpc_free_clone(req);
1206 /* Called from a cleanup function if the device is being cleaned up
1207 forcefully. The exports should all have been disconnected already,
1208 the only thing left to do is
1209 - clear the recovery flags
1211 - free queued requests and replies, but don't send replies
1212 Because the obd_stopping flag is set, no new requests should be received.
1215 void target_cleanup_recovery(struct obd_device *obd)
1217 struct ptlrpc_request *req, *n;
1220 LASSERT(obd->obd_stopping);
1222 spin_lock_bh(&obd->obd_processing_task_lock);
1223 if (!obd->obd_recovering) {
1224 spin_unlock_bh(&obd->obd_processing_task_lock);
1228 obd->obd_recovering = obd->obd_abort_recovery = 0;
1229 target_cancel_recovery_timer(obd);
1230 spin_unlock_bh(&obd->obd_processing_task_lock);
1232 list_for_each_entry_safe(req, n, &obd->obd_req_replay_queue, rq_list) {
1233 LASSERT (req->rq_reply_state == 0);
1234 target_exp_dequeue_req_replay(req);
1235 ptlrpc_free_clone(req);
1237 list_for_each_entry_safe(req, n, &obd->obd_lock_replay_queue, rq_list){
1238 LASSERT (req->rq_reply_state == 0);
1239 ptlrpc_free_clone(req);
1241 list_for_each_entry_safe(req, n, &obd->obd_final_req_queue, rq_list) {
1242 LASSERT (req->rq_reply_state == 0);
1243 ptlrpc_free_clone(req);
1249 /* obd_processing_task_lock should be held */
1250 void target_cancel_recovery_timer(struct obd_device *obd)
1252 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
1253 cfs_timer_disarm(&obd->obd_recovery_timer);
1256 /* extend = 1 means require at least "duration" seconds left in the timer,
1257 extend = 0 means set the total duration (start_recovery_timer) */
1258 static void reset_recovery_timer(struct obd_device *obd, int duration,
1261 cfs_time_t now = cfs_time_current_sec();
1262 cfs_duration_t left;
1264 spin_lock_bh(&obd->obd_processing_task_lock);
1265 if (!obd->obd_recovering || obd->obd_abort_recovery) {
1266 spin_unlock_bh(&obd->obd_processing_task_lock);
1270 left = cfs_time_sub(obd->obd_recovery_end, now);
1272 if (extend && (duration > left))
1273 obd->obd_recovery_timeout += duration - left;
1274 else if (!extend && (duration > obd->obd_recovery_timeout))
1275 /* Track the client's largest expected replay time */
1276 obd->obd_recovery_timeout = duration;
1279 * If total recovery time already exceed the
1280 * obd_recovery_max_time, then CRAY XT3 will
1281 * abort the recovery
1283 if(obd->obd_recovery_timeout > obd->obd_recovery_max_time)
1284 obd->obd_recovery_timeout = obd->obd_recovery_max_time;
1286 obd->obd_recovery_end = obd->obd_recovery_start +
1287 obd->obd_recovery_timeout;
1288 if (!cfs_timer_is_armed(&obd->obd_recovery_timer) ||
1289 cfs_time_before(now, obd->obd_recovery_end)) {
1290 left = cfs_time_sub(obd->obd_recovery_end, now);
1291 cfs_timer_arm(&obd->obd_recovery_timer, cfs_time_shift(left));
1293 spin_unlock_bh(&obd->obd_processing_task_lock);
1294 CDEBUG(D_HA, "%s: recovery timer will expire in %u seconds\n",
1295 obd->obd_name, (unsigned)left);
1298 static void check_and_start_recovery_timer(struct obd_device *obd)
1300 spin_lock_bh(&obd->obd_processing_task_lock);
1301 if (cfs_timer_is_armed(&obd->obd_recovery_timer)) {
1302 spin_unlock_bh(&obd->obd_processing_task_lock);
1305 CWARN("%s: starting recovery timer\n", obd->obd_name);
1306 obd->obd_recovery_start = cfs_time_current_sec();
1308 obd->obd_recovery_timeout = OBD_RECOVERY_FACTOR * obd_timeout;
1309 spin_unlock_bh(&obd->obd_processing_task_lock);
1311 reset_recovery_timer(obd, obd->obd_recovery_timeout, 0);
1314 /* Reset the timer with each new client connection */
1316 * This timer is actually reconnect_timer, which is for making sure
1317 * the total recovery window is at least as big as my reconnect
1318 * attempt timing. So the initial recovery time_out will be set to
1319 * OBD_RECOVERY_FACTOR * obd_timeout. If the timeout coming
1320 * from client is bigger than this, then the recovery time_out will
1321 * be extend to make sure the client could be reconnected, in the
1322 * process, the timeout from the new client should be ignored.
1326 target_start_and_reset_recovery_timer(struct obd_device *obd,
1327 struct ptlrpc_request *req,
1330 int req_timeout = lustre_msg_get_timeout(req->rq_reqmsg);
1332 /* teach server about old server's estimates */
1334 at_add(&req->rq_rqbd->rqbd_service->srv_at_estimate,
1335 at_timeout2est(req_timeout));
1337 check_and_start_recovery_timer(obd);
1339 req_timeout *= OBD_RECOVERY_FACTOR;
1340 if (req_timeout > obd->obd_recovery_timeout && !new_client)
1341 reset_recovery_timer(obd, req_timeout, 0);
1345 static int check_for_next_transno(struct obd_device *obd)
1347 struct ptlrpc_request *req = NULL;
1348 int wake_up = 0, connected, completed, queue_len, max;
1349 __u64 next_transno, req_transno;
1351 spin_lock_bh(&obd->obd_processing_task_lock);
1353 if (!list_empty(&obd->obd_req_replay_queue)) {
1354 req = list_entry(obd->obd_req_replay_queue.next,
1355 struct ptlrpc_request, rq_list);
1356 req_transno = lustre_msg_get_transno(req->rq_reqmsg);
1361 max = obd->obd_max_recoverable_clients;
1362 connected = obd->obd_connected_clients;
1363 completed = connected - obd->obd_recoverable_clients;
1364 queue_len = obd->obd_requests_queued_for_recovery;
1365 next_transno = obd->obd_next_recovery_transno;
1367 CDEBUG(D_HA, "max: %d, connected: %d, completed: %d, queue_len: %d, "
1368 "req_transno: "LPU64", next_transno: "LPU64"\n",
1369 max, connected, completed, queue_len, req_transno, next_transno);
1371 if (obd->obd_abort_recovery) {
1372 CDEBUG(D_HA, "waking for aborted recovery\n");
1374 } else if (atomic_read(&obd->obd_req_replay_clients) == 0) {
1375 CDEBUG(D_HA, "waking for completed recovery\n");
1377 } else if (req_transno == next_transno) {
1378 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1380 } else if (queue_len + completed == max) {
1381 /* handle gaps occured due to lost reply. It is allowed gaps
1382 * because all clients are connected and there will be resend
1383 * for missed transaction */
1384 LASSERTF(req_transno >= next_transno,
1385 "req_transno: "LPU64", next_transno: "LPU64"\n",
1386 req_transno, next_transno);
1388 CDEBUG(req_transno > obd->obd_last_committed ? D_ERROR : D_HA,
1389 "waking for skipped transno (skip: "LPD64
1390 ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
1391 next_transno, queue_len, completed, connected, req_transno);
1392 obd->obd_next_recovery_transno = req_transno;
1394 } else if (OBD_FAIL_CHECK(OBD_FAIL_MDS_RECOVERY_ACCEPTS_GAPS)) {
1395 CDEBUG(D_HA, "accepting transno gaps is explicitly allowed"
1396 " by fail_lock, waking up ("LPD64")\n", next_transno);
1397 obd->obd_next_recovery_transno = req_transno;
1399 } else if (queue_len == atomic_read(&obd->obd_req_replay_clients)) {
1400 /* some clients haven't connected in time, but we can try
1401 * to replay requests that demand on already committed ones
1402 * also, we can replay first non-committed transation */
1403 LASSERT(req_transno != 0);
1404 if (req_transno == obd->obd_last_committed + 1) {
1405 obd->obd_next_recovery_transno = req_transno;
1406 } else if (req_transno > obd->obd_last_committed) {
1407 /* can't continue recovery: have no needed transno */
1408 obd->obd_abort_recovery = 1;
1409 CDEBUG(D_ERROR, "abort due to missed clients. max: %d, "
1410 "connected: %d, completed: %d, queue_len: %d, "
1411 "req_transno: "LPU64", next_transno: "LPU64"\n",
1412 max, connected, completed, queue_len,
1413 req_transno, next_transno);
1418 spin_unlock_bh(&obd->obd_processing_task_lock);
1422 static struct ptlrpc_request *target_next_replay_req(struct obd_device *obd)
1424 struct l_wait_info lwi = { 0 };
1425 struct ptlrpc_request *req;
1427 CDEBUG(D_HA, "Waiting for transno "LPD64"\n",
1428 obd->obd_next_recovery_transno);
1429 l_wait_event(obd->obd_next_transno_waitq,
1430 check_for_next_transno(obd), &lwi);
1432 spin_lock_bh(&obd->obd_processing_task_lock);
1433 if (obd->obd_abort_recovery) {
1435 } else if (!list_empty(&obd->obd_req_replay_queue)) {
1436 req = list_entry(obd->obd_req_replay_queue.next,
1437 struct ptlrpc_request, rq_list);
1438 target_exp_dequeue_req_replay(req);
1439 list_del_init(&req->rq_list);
1440 obd->obd_requests_queued_for_recovery--;
1444 spin_unlock_bh(&obd->obd_processing_task_lock);
1448 static int check_for_next_lock(struct obd_device *obd)
1450 struct ptlrpc_request *req = NULL;
1453 spin_lock_bh(&obd->obd_processing_task_lock);
1454 if (!list_empty(&obd->obd_lock_replay_queue)) {
1455 req = list_entry(obd->obd_lock_replay_queue.next,
1456 struct ptlrpc_request, rq_list);
1457 CDEBUG(D_HA, "waking for next lock\n");
1459 } else if (atomic_read(&obd->obd_lock_replay_clients) == 0) {
1460 CDEBUG(D_HA, "waking for completed lock replay\n");
1462 } else if (obd->obd_abort_recovery) {
1463 CDEBUG(D_HA, "waking for aborted recovery\n");
1466 spin_unlock_bh(&obd->obd_processing_task_lock);
1471 static struct ptlrpc_request *target_next_replay_lock(struct obd_device *obd)
1473 struct l_wait_info lwi = { 0 };
1474 struct ptlrpc_request *req;
1476 CDEBUG(D_HA, "Waiting for lock\n");
1477 l_wait_event(obd->obd_next_transno_waitq,
1478 check_for_next_lock(obd), &lwi);
1480 spin_lock_bh(&obd->obd_processing_task_lock);
1481 if (obd->obd_abort_recovery) {
1483 } else if (!list_empty(&obd->obd_lock_replay_queue)) {
1484 req = list_entry(obd->obd_lock_replay_queue.next,
1485 struct ptlrpc_request, rq_list);
1486 list_del_init(&req->rq_list);
1490 spin_unlock_bh(&obd->obd_processing_task_lock);
1494 static struct ptlrpc_request *target_next_final_ping(struct obd_device *obd)
1496 struct ptlrpc_request *req;
1498 spin_lock_bh(&obd->obd_processing_task_lock);
1499 if (!list_empty(&obd->obd_final_req_queue)) {
1500 req = list_entry(obd->obd_final_req_queue.next,
1501 struct ptlrpc_request, rq_list);
1502 list_del_init(&req->rq_list);
1506 spin_unlock_bh(&obd->obd_processing_task_lock);
1510 static inline int req_replay_done(struct obd_export *exp)
1512 return (exp->exp_req_replay_needed == 0);
1515 static inline int lock_replay_done(struct obd_export *exp)
1517 return (exp->exp_lock_replay_needed == 0);
1520 static inline int connect_done(struct obd_export *exp)
1522 return (exp->exp_in_recovery != 0);
1525 static int check_for_clients(struct obd_device *obd)
1527 if (obd->obd_abort_recovery)
1529 LASSERT(obd->obd_connected_clients <= obd->obd_max_recoverable_clients);
1530 if (obd->obd_no_conn == 0 &&
1531 obd->obd_connected_clients == obd->obd_max_recoverable_clients)
1536 static int handle_recovery_req(struct ptlrpc_thread *thread,
1537 struct ptlrpc_request *req,
1538 svc_handler_t handler)
1543 rc = lu_context_init(&req->rq_session, LCT_SESSION);
1545 CERROR("Failure to initialize session: %d\n", rc);
1548 req->rq_session.lc_thread = thread;
1549 lu_context_enter(&req->rq_session);
1550 req->rq_svc_thread = thread;
1551 req->rq_svc_thread->t_env->le_ses = &req->rq_session;
1553 /* thread context */
1554 lu_context_enter(&thread->t_env->le_ctx);
1556 lu_context_exit(&thread->t_env->le_ctx);
1558 lu_context_exit(&req->rq_session);
1559 lu_context_fini(&req->rq_session);
1560 /* don't reset timer for final stage */
1561 if (!req_replay_done(req->rq_export) ||
1562 !lock_replay_done(req->rq_export))
1563 reset_recovery_timer(class_exp2obd(req->rq_export),
1564 OBD_RECOVERY_FACTOR * AT_OFF ? obd_timeout :
1565 at_get(&req->rq_rqbd->rqbd_service->srv_at_estimate), 1);
1566 ptlrpc_free_clone(req);
1570 static void resume_recovery_timer(struct obd_device *obd)
1572 /* to be safe, make it at least OBD_RECOVERY_FACTOR * obd_timeout */
1573 reset_recovery_timer(obd, OBD_RECOVERY_FACTOR * obd_timeout, 1);
1576 static int target_recovery_thread(void *arg)
1578 struct obd_device *obd = arg;
1579 struct ptlrpc_request *req;
1580 struct target_recovery_data *trd = &obd->obd_recovery_data;
1581 struct l_wait_info lwi = { 0 };
1582 unsigned long delta;
1583 unsigned long flags;
1585 struct ptlrpc_thread fake_svc_thread, *thread = &fake_svc_thread;
1589 cfs_daemonize("tgt_recov");
1591 SIGNAL_MASK_LOCK(current, flags);
1592 sigfillset(¤t->blocked);
1594 SIGNAL_MASK_UNLOCK(current, flags);
1596 rc = lu_context_init(&env.le_ctx, LCT_MD_THREAD);
1600 thread->t_env = &env;
1601 env.le_ctx.lc_thread = thread;
1603 CERROR("%s: started recovery thread pid %d\n", obd->obd_name,
1605 trd->trd_processing_task = current->pid;
1607 obd->obd_recovering = 1;
1608 complete(&trd->trd_starting);
1610 /* first of all, we have to know the first transno to replay */
1611 obd->obd_abort_recovery = 0;
1612 l_wait_event(obd->obd_next_transno_waitq,
1613 check_for_clients(obd), &lwi);
1615 spin_lock_bh(&obd->obd_processing_task_lock);
1616 target_cancel_recovery_timer(obd);
1617 spin_unlock_bh(&obd->obd_processing_task_lock);
1619 /* If some clients haven't connected in time, evict them */
1620 if (obd->obd_abort_recovery) {
1621 CWARN("Some clients haven't connect in time (%d/%d),"
1622 "evict them\n", obd->obd_connected_clients,
1623 obd->obd_max_recoverable_clients);
1624 obd->obd_abort_recovery = obd->obd_stopping;
1625 class_disconnect_stale_exports(obd, connect_done);
1627 /* next stage: replay requests */
1629 obd->obd_req_replaying = 1;
1630 CDEBUG(D_INFO, "1: request replay stage - %d clients from t"LPU64"\n",
1631 atomic_read(&obd->obd_req_replay_clients),
1632 obd->obd_next_recovery_transno);
1633 resume_recovery_timer(obd);
1634 while ((req = target_next_replay_req(obd))) {
1635 LASSERT(trd->trd_processing_task == current->pid);
1636 DEBUG_REQ(D_HA, req, "processing t"LPD64" from %s",
1637 lustre_msg_get_transno(req->rq_reqmsg),
1638 libcfs_nid2str(req->rq_peer.nid));
1639 handle_recovery_req(thread, req,
1640 trd->trd_recovery_handler);
1641 obd->obd_replayed_requests++;
1642 spin_lock_bh(&obd->obd_processing_task_lock);
1643 obd->obd_next_recovery_transno++;
1644 spin_unlock_bh(&obd->obd_processing_task_lock);
1647 spin_lock_bh(&obd->obd_processing_task_lock);
1648 target_cancel_recovery_timer(obd);
1649 spin_unlock_bh(&obd->obd_processing_task_lock);
1651 /* If some clients haven't replayed requests in time, evict them */
1652 if (obd->obd_abort_recovery) {
1653 CDEBUG(D_ERROR, "req replay timed out, aborting ...\n");
1654 obd->obd_abort_recovery = obd->obd_stopping;
1655 class_disconnect_stale_exports(obd, req_replay_done);
1656 abort_req_replay_queue(obd);
1659 /* The second stage: replay locks */
1660 CDEBUG(D_INFO, "2: lock replay stage - %d clients\n",
1661 atomic_read(&obd->obd_lock_replay_clients));
1662 resume_recovery_timer(obd);
1663 while ((req = target_next_replay_lock(obd))) {
1664 LASSERT(trd->trd_processing_task == current->pid);
1665 DEBUG_REQ(D_HA|D_WARNING, req, "processing lock from %s: ",
1666 libcfs_nid2str(req->rq_peer.nid));
1667 handle_recovery_req(thread, req,
1668 trd->trd_recovery_handler);
1669 obd->obd_replayed_locks++;
1672 spin_lock_bh(&obd->obd_processing_task_lock);
1673 target_cancel_recovery_timer(obd);
1674 spin_unlock_bh(&obd->obd_processing_task_lock);
1675 /* If some clients haven't replayed requests in time, evict them */
1676 if (obd->obd_abort_recovery) {
1678 CERROR("lock replay timed out, aborting ...\n");
1679 obd->obd_abort_recovery = obd->obd_stopping;
1680 stale = class_disconnect_stale_exports(obd, lock_replay_done);
1681 abort_lock_replay_queue(obd);
1684 /* We drop recoverying flag to forward all new requests
1685 * to regular mds_handle() since now */
1686 spin_lock_bh(&obd->obd_processing_task_lock);
1687 obd->obd_recovering = obd->obd_abort_recovery = 0;
1688 spin_unlock_bh(&obd->obd_processing_task_lock);
1689 /* The third stage: reply on final pings */
1690 CDEBUG(D_INFO, "3: final stage - process recovery completion pings\n");
1691 while ((req = target_next_final_ping(obd))) {
1692 LASSERT(trd->trd_processing_task == current->pid);
1693 DEBUG_REQ(D_HA, req, "processing final ping from %s: ",
1694 libcfs_nid2str(req->rq_peer.nid));
1695 handle_recovery_req(thread, req,
1696 trd->trd_recovery_handler);
1699 delta = (jiffies - delta) / HZ;
1700 CDEBUG(D_INFO,"4: recovery completed in %lus - %d/%d reqs/locks\n",
1701 delta, obd->obd_replayed_requests, obd->obd_replayed_locks);
1702 LASSERT(atomic_read(&obd->obd_req_replay_clients) == 0);
1703 LASSERT(atomic_read(&obd->obd_lock_replay_clients) == 0);
1704 if (delta > obd_timeout * 2) {
1705 CWARN("too long recovery - read logs\n");
1706 libcfs_debug_dumplog();
1709 target_finish_recovery(obd);
1711 lu_context_fini(&env.le_ctx);
1712 trd->trd_processing_task = 0;
1713 complete(&trd->trd_finishing);
1717 int target_start_recovery_thread(struct obd_device *obd, svc_handler_t handler)
1720 struct target_recovery_data *trd = &obd->obd_recovery_data;
1722 memset(trd, 0, sizeof(*trd));
1723 init_completion(&trd->trd_starting);
1724 init_completion(&trd->trd_finishing);
1725 trd->trd_recovery_handler = handler;
1727 if (kernel_thread(target_recovery_thread, obd, 0) > 0) {
1728 wait_for_completion(&trd->trd_starting);
1729 LASSERT(obd->obd_recovering != 0);
1736 void target_stop_recovery_thread(struct obd_device *obd)
1738 spin_lock_bh(&obd->obd_processing_task_lock);
1739 if (obd->obd_recovery_data.trd_processing_task > 0) {
1740 struct target_recovery_data *trd = &obd->obd_recovery_data;
1741 CERROR("%s: Aborting recovery\n", obd->obd_name);
1742 obd->obd_abort_recovery = 1;
1743 wake_up(&obd->obd_next_transno_waitq);
1744 spin_unlock_bh(&obd->obd_processing_task_lock);
1745 wait_for_completion(&trd->trd_finishing);
1747 spin_unlock_bh(&obd->obd_processing_task_lock);
1751 void target_recovery_fini(struct obd_device *obd)
1753 class_disconnect_exports(obd);
1754 target_stop_recovery_thread(obd);
1755 target_cleanup_recovery(obd);
1757 EXPORT_SYMBOL(target_recovery_fini);
1759 static void target_recovery_expired(unsigned long castmeharder)
1761 struct obd_device *obd = (struct obd_device *)castmeharder;
1762 LCONSOLE_WARN("%s: recovery timed out; %d clients never reconnected "
1763 "after %lds (%d clients did)\n",
1764 obd->obd_name, obd->obd_recoverable_clients,
1765 cfs_time_current_sec()- obd->obd_recovery_start,
1766 obd->obd_connected_clients);
1767 spin_lock_bh(&obd->obd_processing_task_lock);
1768 if (obd->obd_recovering)
1769 obd->obd_abort_recovery = 1;
1770 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1771 spin_unlock_bh(&obd->obd_processing_task_lock);
1774 void target_recovery_init(struct obd_device *obd, svc_handler_t handler)
1776 if (obd->obd_max_recoverable_clients == 0)
1779 CWARN("RECOVERY: service %s, %d recoverable clients, "
1780 "last_transno "LPU64"\n", obd->obd_name,
1781 obd->obd_max_recoverable_clients, obd->obd_last_committed);
1782 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
1783 obd->obd_recovery_start = 0;
1784 obd->obd_recovery_end = 0;
1785 obd->obd_recovery_timeout = OBD_RECOVERY_FACTOR * obd_timeout;
1786 /* bz13079: this should be set to desired value for ost but not for mds */
1787 obd->obd_recovery_max_time = OBD_RECOVERY_MAX_TIME;
1788 cfs_timer_init(&obd->obd_recovery_timer, target_recovery_expired, obd);
1789 target_start_recovery_thread(obd, handler);
1791 EXPORT_SYMBOL(target_recovery_init);
1795 int target_process_req_flags(struct obd_device *obd, struct ptlrpc_request *req)
1797 struct obd_export *exp = req->rq_export;
1798 LASSERT(exp != NULL);
1799 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
1800 /* client declares he's ready to replay locks */
1801 spin_lock_bh(&obd->obd_processing_task_lock);
1802 if (exp->exp_req_replay_needed) {
1803 LASSERT(atomic_read(&obd->obd_req_replay_clients) > 0);
1804 spin_lock(&exp->exp_lock);
1805 exp->exp_req_replay_needed = 0;
1806 spin_unlock(&exp->exp_lock);
1807 atomic_dec(&obd->obd_req_replay_clients);
1808 LASSERT(obd->obd_recoverable_clients > 0);
1809 obd->obd_recoverable_clients--;
1810 if (atomic_read(&obd->obd_req_replay_clients) == 0)
1811 CDEBUG(D_HA, "all clients have replayed reqs\n");
1812 wake_up(&obd->obd_next_transno_waitq);
1814 spin_unlock_bh(&obd->obd_processing_task_lock);
1816 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
1817 /* client declares he's ready to complete recovery
1818 * so, we put the request on th final queue */
1819 spin_lock_bh(&obd->obd_processing_task_lock);
1820 if (exp->exp_lock_replay_needed) {
1821 LASSERT(atomic_read(&obd->obd_lock_replay_clients) > 0);
1822 spin_lock(&exp->exp_lock);
1823 exp->exp_lock_replay_needed = 0;
1824 spin_unlock(&exp->exp_lock);
1825 atomic_dec(&obd->obd_lock_replay_clients);
1826 if (atomic_read(&obd->obd_lock_replay_clients) == 0)
1827 CDEBUG(D_HA, "all clients have replayed locks\n");
1828 wake_up(&obd->obd_next_transno_waitq);
1830 spin_unlock_bh(&obd->obd_processing_task_lock);
1836 int target_queue_recovery_request(struct ptlrpc_request *req,
1837 struct obd_device *obd)
1839 struct list_head *tmp;
1841 __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
1845 if (obd->obd_recovery_data.trd_processing_task == cfs_curproc_pid()) {
1846 /* Processing the queue right now, don't re-add. */
1850 target_process_req_flags(obd, req);
1852 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
1853 /* client declares he's ready to complete recovery
1854 * so, we put the request on th final queue */
1855 req = ptlrpc_clone_req(req);
1858 DEBUG_REQ(D_HA, req, "queue final req");
1859 spin_lock_bh(&obd->obd_processing_task_lock);
1860 if (obd->obd_recovering)
1861 list_add_tail(&req->rq_list, &obd->obd_final_req_queue);
1863 spin_unlock_bh(&obd->obd_processing_task_lock);
1864 ptlrpc_free_clone(req);
1865 if (obd->obd_stopping) {
1871 spin_unlock_bh(&obd->obd_processing_task_lock);
1874 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
1875 /* client declares he's ready to replay locks */
1876 req = ptlrpc_clone_req(req);
1879 DEBUG_REQ(D_HA, req, "queue lock replay req");
1880 spin_lock_bh(&obd->obd_processing_task_lock);
1881 LASSERT(obd->obd_recovering);
1882 /* usually due to recovery abort */
1883 if (!req->rq_export->exp_in_recovery) {
1884 spin_unlock_bh(&obd->obd_processing_task_lock);
1885 ptlrpc_free_clone(req);
1888 LASSERT(req->rq_export->exp_lock_replay_needed);
1889 list_add_tail(&req->rq_list, &obd->obd_lock_replay_queue);
1890 spin_unlock_bh(&obd->obd_processing_task_lock);
1891 wake_up(&obd->obd_next_transno_waitq);
1895 /* CAVEAT EMPTOR: The incoming request message has been swabbed
1896 * (i.e. buflens etc are in my own byte order), but type-dependent
1897 * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1900 CFS_INIT_LIST_HEAD(&req->rq_list);
1901 DEBUG_REQ(D_HA, req, "not queueing");
1905 spin_lock_bh(&obd->obd_processing_task_lock);
1907 /* If we're processing the queue, we want don't want to queue this
1910 * Also, if this request has a transno less than the one we're waiting
1911 * for, we should process it now. It could (and currently always will)
1912 * be an open request for a descriptor that was opened some time ago.
1914 * Also, a resent, replayed request that has already been
1915 * handled will pass through here and be processed immediately.
1917 CWARN("Next recovery transno: "LPU64", current: "LPU64", replaying: %i\n",
1918 obd->obd_next_recovery_transno, transno, obd->obd_req_replaying);
1919 if (transno < obd->obd_next_recovery_transno && obd->obd_req_replaying) {
1920 /* Processing the queue right now, don't re-add. */
1921 LASSERT(list_empty(&req->rq_list));
1922 spin_unlock_bh(&obd->obd_processing_task_lock);
1925 spin_unlock_bh(&obd->obd_processing_task_lock);
1927 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_REPLAY_DROP))
1930 req = ptlrpc_clone_req(req);
1934 spin_lock_bh(&obd->obd_processing_task_lock);
1935 LASSERT(obd->obd_recovering);
1936 if (!req->rq_export->exp_in_recovery) {
1937 spin_unlock_bh(&obd->obd_processing_task_lock);
1938 ptlrpc_free_clone(req);
1941 LASSERT(req->rq_export->exp_req_replay_needed);
1943 if (target_exp_enqueue_req_replay(req)) {
1944 spin_unlock_bh(&obd->obd_processing_task_lock);
1945 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1946 ptlrpc_free_clone(req);
1951 list_for_each(tmp, &obd->obd_req_replay_queue) {
1952 struct ptlrpc_request *reqiter =
1953 list_entry(tmp, struct ptlrpc_request, rq_list);
1955 if (lustre_msg_get_transno(reqiter->rq_reqmsg) > transno) {
1956 list_add_tail(&req->rq_list, &reqiter->rq_list);
1961 if (unlikely(lustre_msg_get_transno(reqiter->rq_reqmsg) ==
1963 DEBUG_REQ(D_ERROR, req, "dropping replay: transno "
1964 "has been claimed by another client");
1965 spin_unlock_bh(&obd->obd_processing_task_lock);
1966 target_exp_dequeue_req_replay(req);
1967 ptlrpc_free_clone(req);
1973 list_add_tail(&req->rq_list, &obd->obd_req_replay_queue);
1975 obd->obd_requests_queued_for_recovery++;
1976 wake_up(&obd->obd_next_transno_waitq);
1977 spin_unlock_bh(&obd->obd_processing_task_lock);
1981 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1983 return req->rq_export->exp_obd;
1986 static inline struct ldlm_pool *ldlm_exp2pl(struct obd_export *exp)
1988 LASSERT(exp != NULL);
1989 return &exp->exp_obd->obd_namespace->ns_pool;
1993 * Packs current SLV and Limit into \a req.
1995 int target_pack_pool_reply(struct ptlrpc_request *req)
1997 struct obd_device *obd;
2001 * Check that we still have all structures alive as this may
2002 * be some late rpc in shutdown time.
2004 if (unlikely(!req->rq_export || !req->rq_export->exp_obd ||
2005 !exp_connect_lru_resize(req->rq_export))) {
2006 lustre_msg_set_slv(req->rq_repmsg, 0);
2007 lustre_msg_set_limit(req->rq_repmsg, 0);
2012 * OBD is alive here as export is alive, which we checked above.
2014 obd = req->rq_export->exp_obd;
2016 read_lock(&obd->obd_pool_lock);
2017 lustre_msg_set_slv(req->rq_repmsg, obd->obd_pool_slv);
2018 lustre_msg_set_limit(req->rq_repmsg, obd->obd_pool_limit);
2019 read_unlock(&obd->obd_pool_lock);
2024 int target_send_reply_msg(struct ptlrpc_request *req, int rc, int fail_id)
2026 if (OBD_FAIL_CHECK_ORSET(fail_id & ~OBD_FAIL_ONCE, OBD_FAIL_ONCE)) {
2027 DEBUG_REQ(D_ERROR, req, "dropping reply");
2032 DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
2033 req->rq_status = rc;
2034 return (ptlrpc_send_error(req, 1));
2036 DEBUG_REQ(D_NET, req, "sending reply");
2039 return (ptlrpc_send_reply(req, PTLRPC_REPLY_MAYBE_DIFFICULT));
2042 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
2045 struct ptlrpc_reply_state *rs;
2046 struct obd_device *obd;
2047 struct obd_export *exp;
2048 struct ptlrpc_service *svc;
2051 if (req->rq_no_reply) {
2056 svc = req->rq_rqbd->rqbd_service;
2057 rs = req->rq_reply_state;
2058 if (rs == NULL || !rs->rs_difficult) {
2060 target_send_reply_msg (req, rc, fail_id);
2065 /* must be an export if locks saved */
2066 LASSERT (req->rq_export != NULL);
2067 /* req/reply consistent */
2068 LASSERT (rs->rs_service == svc);
2071 LASSERT (!rs->rs_scheduled);
2072 LASSERT (!rs->rs_scheduled_ever);
2073 LASSERT (!rs->rs_handled);
2074 LASSERT (!rs->rs_on_net);
2075 LASSERT (rs->rs_export == NULL);
2076 LASSERT (list_empty(&rs->rs_obd_list));
2077 LASSERT (list_empty(&rs->rs_exp_list));
2079 exp = class_export_get (req->rq_export);
2082 /* disable reply scheduling onto srv_reply_queue while I'm setting up */
2083 rs->rs_scheduled = 1;
2085 rs->rs_xid = req->rq_xid;
2086 rs->rs_transno = req->rq_transno;
2087 rs->rs_export = exp;
2089 spin_lock(&obd->obd_uncommitted_replies_lock);
2091 CDEBUG(D_NET, "rs transno = "LPU64", last committed = "LPU64"\n",
2092 rs->rs_transno, obd->obd_last_committed);
2093 if (rs->rs_transno > obd->obd_last_committed) {
2094 /* not committed already */
2095 list_add_tail (&rs->rs_obd_list,
2096 &obd->obd_uncommitted_replies);
2099 spin_unlock (&obd->obd_uncommitted_replies_lock);
2100 spin_lock (&exp->exp_lock);
2102 list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
2104 spin_unlock(&exp->exp_lock);
2106 netrc = target_send_reply_msg (req, rc, fail_id);
2108 spin_lock(&svc->srv_lock);
2110 svc->srv_n_difficult_replies++;
2113 /* error sending: reply is off the net. Also we need +1
2114 * reply ref until ptlrpc_server_handle_reply() is done
2115 * with the reply state (if the send was successful, there
2116 * would have been +1 ref for the net, which
2117 * reply_out_callback leaves alone) */
2119 ptlrpc_rs_addref(rs);
2120 atomic_inc (&svc->srv_outstanding_replies);
2123 if (rs->rs_transno <= obd->obd_last_committed ||
2124 (!rs->rs_on_net && !rs->rs_no_ack) ||
2125 list_empty(&rs->rs_exp_list) || /* completed already */
2126 list_empty(&rs->rs_obd_list)) {
2127 CDEBUG(D_HA, "Schedule reply immediately\n");
2128 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
2129 cfs_waitq_signal (&svc->srv_waitq);
2131 list_add (&rs->rs_list, &svc->srv_active_replies);
2132 rs->rs_scheduled = 0; /* allow notifier to schedule */
2135 spin_unlock(&svc->srv_lock);
2139 int target_handle_ping(struct ptlrpc_request *req)
2141 obd_ping(req->rq_export);
2142 return req_capsule_server_pack(&req->rq_pill);
2145 void target_committed_to_req(struct ptlrpc_request *req)
2147 struct obd_device *obd;
2149 if (req == NULL || req->rq_export == NULL)
2152 obd = req->rq_export->exp_obd;
2156 if (!obd->obd_no_transno && req->rq_repmsg != NULL)
2157 lustre_msg_set_last_committed(req->rq_repmsg,
2158 obd->obd_last_committed);
2160 DEBUG_REQ(D_IOCTL, req, "not sending last_committed update (%d/"
2161 "%d)", obd->obd_no_transno, req->rq_repmsg == NULL);
2163 CDEBUG(D_INFO, "last_committed "LPU64", transno "LPU64", xid "LPU64"\n",
2164 obd->obd_last_committed, req->rq_transno, req->rq_xid);
2167 EXPORT_SYMBOL(target_committed_to_req);
2169 #ifdef HAVE_QUOTA_SUPPORT
2170 int target_handle_qc_callback(struct ptlrpc_request *req)
2172 struct obd_quotactl *oqctl;
2173 struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
2175 oqctl = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
2179 cli->cl_qchk_stat = oqctl->qc_stat;
2184 int target_handle_dqacq_callback(struct ptlrpc_request *req)
2187 struct obd_device *obd = req->rq_export->exp_obd;
2188 struct obd_device *master_obd;
2189 struct lustre_quota_ctxt *qctxt;
2190 struct qunit_data *qdata;
2192 struct qunit_data_old *qdata_old;
2196 rc = req_capsule_server_pack(&req->rq_pill);
2198 CERROR("packing reply failed!: rc = %d\n", rc);
2202 LASSERT(req->rq_export);
2204 /* fixed for bug10707 */
2205 if ((req->rq_export->exp_connect_flags & OBD_CONNECT_QUOTA64) &&
2206 !OBD_FAIL_CHECK(OBD_FAIL_QUOTA_QD_COUNT_32BIT)) {
2207 CDEBUG(D_QUOTA, "qd_count is 64bit!\n");
2208 rep = req_capsule_server_get(&req->rq_pill,
2211 qdata = req_capsule_client_swab_get(&req->rq_pill,
2213 (void*)lustre_swab_qdata);
2215 CDEBUG(D_QUOTA, "qd_count is 32bit!\n");
2216 rep = req_capsule_server_get(&req->rq_pill, &RMF_QUNIT_DATA);
2218 qdata_old = req_capsule_client_swab_get(&req->rq_pill,
2220 (void*)lustre_swab_qdata_old);
2221 qdata = lustre_quota_old_to_new(qdata_old);
2227 /* we use the observer */
2228 LASSERT(obd->obd_observer && obd->obd_observer->obd_observer);
2229 master_obd = obd->obd_observer->obd_observer;
2230 qctxt = &master_obd->u.obt.obt_qctxt;
2232 LASSERT(qctxt->lqc_handler);
2233 rc = qctxt->lqc_handler(master_obd, qdata,
2234 lustre_msg_get_opc(req->rq_reqmsg));
2235 if (rc && rc != -EDQUOT)
2236 CDEBUG(rc == -EBUSY ? D_QUOTA : D_ERROR,
2237 "dqacq failed! (rc:%d)\n", rc);
2239 /* the qd_count might be changed in lqc_handler */
2240 if ((req->rq_export->exp_connect_flags & OBD_CONNECT_QUOTA64) &&
2241 !OBD_FAIL_CHECK(OBD_FAIL_QUOTA_QD_COUNT_32BIT)) {
2242 memcpy(rep, qdata, sizeof(*qdata));
2244 qdata_old = lustre_quota_new_to_old(qdata);
2245 memcpy(rep, qdata_old, sizeof(*qdata_old));
2247 req->rq_status = rc;
2248 rc = ptlrpc_reply(req);
2253 #endif /* !__KERNEL__ */
2255 #endif /* HAVE_QUOTA_SUPPORT */
2257 ldlm_mode_t lck_compat_array[] = {
2258 [LCK_EX] LCK_COMPAT_EX,
2259 [LCK_PW] LCK_COMPAT_PW,
2260 [LCK_PR] LCK_COMPAT_PR,
2261 [LCK_CW] LCK_COMPAT_CW,
2262 [LCK_CR] LCK_COMPAT_CR,
2263 [LCK_NL] LCK_COMPAT_NL,
2264 [LCK_GROUP] LCK_COMPAT_GROUP,
2265 [LCK_COS] LCK_COMPAT_COS,
2269 * Rather arbitrary mapping from LDLM error codes to errno values. This should
2270 * not escape to the user level.
2272 int ldlm_error2errno(ldlm_error_t error)
2280 case ELDLM_LOCK_CHANGED:
2283 case ELDLM_LOCK_ABORTED:
2286 case ELDLM_LOCK_REPLACED:
2289 case ELDLM_NO_LOCK_DATA:
2292 case ELDLM_NAMESPACE_EXISTS:
2295 case ELDLM_BAD_NAMESPACE:
2299 if (((int)error) < 0) /* cast to signed type */
2300 result = error; /* as ldlm_error_t can be unsigned */
2302 CERROR("Invalid DLM result code: %i\n", error);
2308 EXPORT_SYMBOL(ldlm_error2errno);
2311 * Dual to ldlm_error2errno(): maps errno values back to ldlm_error_t.
2313 ldlm_error_t ldlm_errno2error(int err_no)
2322 error = ELDLM_LOCK_CHANGED;
2325 error = ELDLM_LOCK_ABORTED;
2328 error = ELDLM_LOCK_REPLACED;
2331 error = ELDLM_NO_LOCK_DATA;
2334 error = ELDLM_NAMESPACE_EXISTS;
2337 error = ELDLM_BAD_NAMESPACE;
2344 EXPORT_SYMBOL(ldlm_errno2error);