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 Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 # define EXPORT_SYMTAB
25 #define DEBUG_SUBSYSTEM S_LDLM
28 # include <linux/module.h>
30 # include <liblustre.h>
32 #include <linux/obd.h>
33 #include <linux/obd_ost.h> /* for LUSTRE_OSC_NAME */
34 #include <linux/lustre_mds.h> /* for LUSTRE_MDC_NAME */
35 #include <linux/lustre_mgmt.h>
36 #include <linux/lustre_dlm.h>
37 #include <linux/lustre_net.h>
39 /* @priority: if non-zero, move the selected to the list head
40 * @create: if zero, only search in existed connections
42 static int import_set_conn(struct obd_import *imp, struct obd_uuid *uuid,
43 int priority, int create)
45 struct ptlrpc_connection *ptlrpc_conn;
46 struct obd_import_conn *imp_conn = NULL, *item;
50 if (!create && !priority) {
51 CDEBUG(D_HA, "Nothing to do\n");
55 ptlrpc_conn = ptlrpc_uuid_to_connection(uuid);
57 CDEBUG(D_HA, "can't find connection %s\n", uuid->uuid);
62 OBD_ALLOC(imp_conn, sizeof(*imp_conn));
64 GOTO(out_put, rc = -ENOMEM);
68 spin_lock(&imp->imp_lock);
69 list_for_each_entry(item, &imp->imp_conn_list, oic_item) {
70 if (obd_uuid_equals(uuid, &item->oic_uuid)) {
72 list_del(&item->oic_item);
73 list_add(&item->oic_item, &imp->imp_conn_list);
74 item->oic_last_attempt = 0;
76 CDEBUG(D_HA, "imp %p@%s: found existing conn %s%s\n",
77 imp, imp->imp_obd->obd_name, uuid->uuid,
78 (priority ? ", moved to head" : ""));
79 spin_unlock(&imp->imp_lock);
80 GOTO(out_free, rc = 0);
85 imp_conn->oic_conn = ptlrpc_conn;
86 imp_conn->oic_uuid = *uuid;
87 imp_conn->oic_last_attempt = 0;
89 list_add(&imp_conn->oic_item, &imp->imp_conn_list);
91 list_add_tail(&imp_conn->oic_item, &imp->imp_conn_list);
92 CDEBUG(D_HA, "imp %p@%s: add connection %s at %s\n",
93 imp, imp->imp_obd->obd_name, uuid->uuid,
94 (priority ? "head" : "tail"));
96 spin_unlock(&imp->imp_lock);
97 GOTO(out_free, rc = -ENOENT);
101 spin_unlock(&imp->imp_lock);
105 OBD_FREE(imp_conn, sizeof(*imp_conn));
107 ptlrpc_put_connection(ptlrpc_conn);
111 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid)
113 return import_set_conn(imp, uuid, 1, 0);
116 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
119 return import_set_conn(imp, uuid, priority, 1);
122 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
124 struct obd_import_conn *imp_conn;
125 struct obd_export *dlmexp;
129 spin_lock(&imp->imp_lock);
130 if (list_empty(&imp->imp_conn_list)) {
131 LASSERT(!imp->imp_conn_current);
132 LASSERT(!imp->imp_connection);
136 list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
137 if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
139 LASSERT(imp_conn->oic_conn);
141 /* is current conn? */
142 if (imp_conn == imp->imp_conn_current) {
143 LASSERT(imp_conn->oic_conn == imp->imp_connection);
145 if (imp->imp_state != LUSTRE_IMP_CLOSED &&
146 imp->imp_state != LUSTRE_IMP_DISCON) {
147 CERROR("can't remove current connection\n");
148 GOTO(out, rc = -EBUSY);
151 ptlrpc_put_connection(imp->imp_connection);
152 imp->imp_connection = NULL;
154 dlmexp = class_conn2export(&imp->imp_dlm_handle);
155 if (dlmexp && dlmexp->exp_connection) {
156 LASSERT(dlmexp->exp_connection ==
158 ptlrpc_put_connection(dlmexp->exp_connection);
159 dlmexp->exp_connection = NULL;
163 list_del(&imp_conn->oic_item);
164 ptlrpc_put_connection(imp_conn->oic_conn);
165 OBD_FREE(imp_conn, sizeof(*imp_conn));
166 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
167 imp, imp->imp_obd->obd_name, uuid->uuid);
172 spin_unlock(&imp->imp_lock);
174 CERROR("connection %s not found\n", uuid->uuid);
178 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
180 struct lustre_cfg* lcfg = buf;
181 struct client_obd *cli = &obddev->u.cli;
182 struct obd_import *imp;
183 struct obd_uuid server_uuid;
184 int rq_portal, rp_portal, connect_op;
185 char *name = obddev->obd_type->typ_name;
186 char *mgmt_name = NULL;
188 struct obd_device *mgmt_obd;
189 mgmtcli_register_for_events_t register_f;
192 /* In a more perfect world, we would hang a ptlrpc_client off of
193 * obd_type and just use the values from there. */
194 if (!strcmp(name, LUSTRE_OSC_NAME)) {
195 rq_portal = OST_REQUEST_PORTAL;
196 rp_portal = OSC_REPLY_PORTAL;
197 connect_op = OST_CONNECT;
198 } else if (!strcmp(name, LUSTRE_MDC_NAME)) {
199 rq_portal = MDS_REQUEST_PORTAL;
200 rp_portal = MDC_REPLY_PORTAL;
201 connect_op = MDS_CONNECT;
202 } else if (!strcmp(name, LUSTRE_MGMTCLI_NAME)) {
203 rq_portal = MGMT_REQUEST_PORTAL;
204 rp_portal = MGMT_REPLY_PORTAL;
205 connect_op = MGMT_CONNECT;
207 CERROR("unknown client OBD type \"%s\", can't setup\n",
212 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
213 CERROR("requires a TARGET UUID\n");
217 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
218 CERROR("client UUID must be less than 38 characters\n");
222 if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
223 CERROR("setup requires a SERVER UUID\n");
227 if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
228 CERROR("target UUID must be less than 38 characters\n");
232 sema_init(&cli->cl_sem, 1);
233 cli->cl_conn_count = 0;
234 memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
235 min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
236 sizeof(server_uuid)));
239 cli->cl_avail_grant = 0;
240 /* FIXME: should limit this for the sum of all cl_dirty_max */
241 cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
242 if (cli->cl_dirty_max >> PAGE_SHIFT > num_physpages / 8)
243 cli->cl_dirty_max = num_physpages << (PAGE_SHIFT - 3);
244 INIT_LIST_HEAD(&cli->cl_cache_waiters);
245 INIT_LIST_HEAD(&cli->cl_loi_ready_list);
246 INIT_LIST_HEAD(&cli->cl_loi_write_list);
247 INIT_LIST_HEAD(&cli->cl_loi_read_list);
248 spin_lock_init(&cli->cl_loi_list_lock);
249 cli->cl_r_in_flight = 0;
250 cli->cl_w_in_flight = 0;
251 spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
252 spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
253 spin_lock_init(&cli->cl_read_page_hist.oh_lock);
254 spin_lock_init(&cli->cl_write_page_hist.oh_lock);
255 spin_lock_init(&cli->cl_read_offset_hist.oh_lock);
256 spin_lock_init(&cli->cl_write_offset_hist.oh_lock);
257 if (num_physpages >> (20 - PAGE_SHIFT) <= 128) { /* <= 128 MB */
258 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 4;
259 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 4;
260 } else if (num_physpages >> (20 - PAGE_SHIFT) <= 512) { /* <= 512 MB */
261 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 2;
262 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 2;
264 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES;
265 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
270 CERROR("ldlm_get_ref failed: %d\n", rc);
274 ptlrpc_init_client(rq_portal, rp_portal, name,
275 &obddev->obd_ldlm_client);
277 imp = class_new_import();
279 GOTO(err_ldlm, rc = -ENOENT);
280 imp->imp_client = &obddev->obd_ldlm_client;
281 imp->imp_obd = obddev;
282 imp->imp_connect_op = connect_op;
283 imp->imp_generation = 0;
284 imp->imp_initial_recov = 1;
285 INIT_LIST_HEAD(&imp->imp_pinger_chain);
286 memcpy(imp->imp_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
287 LUSTRE_CFG_BUFLEN(lcfg, 1));
288 class_import_put(imp);
290 rc = client_import_add_conn(imp, &server_uuid, 1);
292 CERROR("can't add initial connection\n");
293 GOTO(err_import, rc);
296 cli->cl_import = imp;
297 /* cli->cl_max_mds_{easize,cookiesize} updated by mdc_init_ea_size() */
298 cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
299 cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
300 cli->cl_sandev = to_kdev_t(0);
302 if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
303 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
304 CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
305 name, obddev->obd_name,
306 imp->imp_target_uuid.uuid);
307 imp->imp_invalid = 1;
309 if (LUSTRE_CFG_BUFLEN(lcfg, 4) > 0)
310 mgmt_name = lustre_cfg_string(lcfg, 4);
312 mgmt_name = lustre_cfg_string(lcfg, 3);
316 if (mgmt_name != NULL) {
317 /* Register with management client if we need to. */
318 CDEBUG(D_HA, "%s registering with %s for events about %s\n",
319 obddev->obd_name, mgmt_name, server_uuid.uuid);
321 mgmt_obd = class_name2obd(mgmt_name);
323 CERROR("can't find mgmtcli %s to register\n",
325 GOTO(err_import, rc = -ENOSYS);
328 register_f = inter_module_get("mgmtcli_register_for_events");
330 CERROR("can't i_m_g mgmtcli_register_for_events\n");
331 GOTO(err_import, rc = -ENOSYS);
334 rc = register_f(mgmt_obd, obddev, &imp->imp_target_uuid);
335 inter_module_put("mgmtcli_register_for_events");
338 cli->cl_mgmtcli_obd = mgmt_obd;
341 spin_lock_init(&cli->cl_qchk_lock);
342 cli->cl_qchk_stat = CL_NO_QUOTACHECK;
347 class_destroy_import(imp);
355 int client_obd_cleanup(struct obd_device *obddev)
357 struct client_obd *cli = &obddev->u.cli;
361 if (cli->cl_mgmtcli_obd) {
362 mgmtcli_deregister_for_events_t dereg_f;
364 dereg_f = inter_module_get("mgmtcli_deregister_for_events");
365 dereg_f(cli->cl_mgmtcli_obd, obddev);
366 inter_module_put("mgmtcli_deregister_for_events");
368 class_destroy_import(cli->cl_import);
369 cli->cl_import = NULL;
371 ldlm_put_ref(obddev->obd_force);
376 int client_connect_import(struct lustre_handle *dlm_handle,
377 struct obd_device *obd, struct obd_uuid *cluuid,
378 struct obd_connect_data *data)
380 struct client_obd *cli = &obd->u.cli;
381 struct obd_import *imp = cli->cl_import;
382 struct obd_export *exp;
387 rc = class_connect(dlm_handle, obd, cluuid);
391 cli->cl_conn_count++;
392 if (cli->cl_conn_count > 1)
394 exp = class_conn2export(dlm_handle);
396 if (obd->obd_namespace != NULL)
397 CERROR("already have namespace!\n");
398 obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
399 LDLM_NAMESPACE_CLIENT);
400 if (obd->obd_namespace == NULL)
401 GOTO(out_disco, rc = -ENOMEM);
403 imp->imp_dlm_handle = *dlm_handle;
404 rc = ptlrpc_init_import(imp);
409 memcpy(&imp->imp_connect_data, data, sizeof(*data));
410 rc = ptlrpc_connect_import(imp, NULL);
412 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
415 LASSERT(exp->exp_connection);
417 ptlrpc_pinger_add_import(imp);
422 ldlm_namespace_free(obd->obd_namespace, 0);
423 obd->obd_namespace = NULL;
425 cli->cl_conn_count--;
426 class_disconnect(exp);
428 class_export_put(exp);
435 int client_disconnect_export(struct obd_export *exp)
437 struct obd_device *obd = class_exp2obd(exp);
438 struct client_obd *cli = &obd->u.cli;
439 struct obd_import *imp = cli->cl_import;
444 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
445 exp, exp ? exp->exp_handle.h_cookie : -1);
450 if (!cli->cl_conn_count) {
451 CERROR("disconnecting disconnected device (%s)\n",
453 GOTO(out_sem, rc = -EINVAL);
456 cli->cl_conn_count--;
457 if (cli->cl_conn_count)
458 GOTO(out_no_disconnect, rc = 0);
460 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
461 * delete it regardless. (It's safe to delete an import that was
463 (void)ptlrpc_pinger_del_import(imp);
465 if (obd->obd_namespace != NULL) {
466 /* obd_no_recov == local only */
467 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
468 obd->obd_no_recov, NULL);
469 ldlm_namespace_free(obd->obd_namespace, obd->obd_no_recov);
470 obd->obd_namespace = NULL;
473 /* Yeah, obd_no_recov also (mainly) means "forced shutdown". */
474 if (obd->obd_no_recov)
475 ptlrpc_invalidate_import(imp);
477 rc = ptlrpc_disconnect_import(imp);
481 err = class_disconnect(exp);
489 /* --------------------------------------------------------------------------
490 * from old lib/target.c
491 * -------------------------------------------------------------------------- */
493 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
494 struct obd_uuid *cluuid)
496 if (exp->exp_connection) {
497 struct lustre_handle *hdl;
498 hdl = &exp->exp_imp_reverse->imp_remote_handle;
499 /* Might be a re-connect after a partition. */
500 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
501 CERROR("%s reconnecting\n", cluuid->uuid);
502 conn->cookie = exp->exp_handle.h_cookie;
503 /* target_handle_connect() treats EALREADY and
504 * -EALREADY differently */
507 CERROR("%s reconnecting from %s, "
508 "handle mismatch (ours "LPX64", theirs "
509 LPX64")\n", cluuid->uuid,
510 exp->exp_connection->c_remote_uuid.uuid,
511 hdl->cookie, conn->cookie);
512 memset(conn, 0, sizeof *conn);
513 /* target_handle_connect() treats EALREADY and
514 * -EALREADY differently */
519 conn->cookie = exp->exp_handle.h_cookie;
520 CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
522 CDEBUG(D_IOCTL,"connect: cookie "LPX64"\n", conn->cookie);
526 int target_handle_connect(struct ptlrpc_request *req, svc_handler_t handler)
528 struct obd_device *target;
529 struct obd_export *export = NULL;
530 struct obd_import *revimp;
531 struct lustre_handle conn;
532 struct obd_uuid tgtuuid;
533 struct obd_uuid cluuid;
534 struct obd_uuid remote_uuid;
537 int rc = 0, abort_recovery;
539 struct obd_connect_data *data;
540 int size = sizeof(*data);
543 OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
545 LASSERT_REQSWAB (req, 0);
546 str = lustre_msg_string(req->rq_reqmsg, 0, sizeof(tgtuuid) - 1);
548 CERROR("bad target UUID for connect\n");
549 GOTO(out, rc = -EINVAL);
552 obd_str2uuid (&tgtuuid, str);
553 target = class_uuid2obd(&tgtuuid);
555 target = class_name2obd(str);
558 if (!target || target->obd_stopping || !target->obd_set_up) {
559 CERROR("UUID '%s' is not available for connect\n", str);
560 GOTO(out, rc = -ENODEV);
563 LASSERT_REQSWAB (req, 1);
564 str = lustre_msg_string(req->rq_reqmsg, 1, sizeof(cluuid) - 1);
566 CERROR("bad client UUID for connect\n");
567 GOTO(out, rc = -EINVAL);
570 obd_str2uuid (&cluuid, str);
572 /* XXX extract a nettype and format accordingly */
573 switch (sizeof(ptl_nid_t)) {
574 /* NB the casts only avoid compiler warnings */
576 snprintf(remote_uuid.uuid, sizeof remote_uuid,
577 "NET_"LPX64"_UUID", (__u64)req->rq_peer.peer_id.nid);
580 snprintf(remote_uuid.uuid, sizeof remote_uuid,
581 "NET_%x_UUID", (__u32)req->rq_peer.peer_id.nid);
587 spin_lock_bh(&target->obd_processing_task_lock);
588 abort_recovery = target->obd_abort_recovery;
589 spin_unlock_bh(&target->obd_processing_task_lock);
591 target_abort_recovery(target);
593 tmp = lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn);
595 GOTO(out, rc = -EPROTO);
597 memcpy(&conn, tmp, sizeof conn);
599 data = lustre_swab_reqbuf(req, 3, sizeof(*data), lustre_swab_connect);
601 GOTO(out, rc = -EPROTO);
603 rc = lustre_pack_reply(req, 1, &size, NULL);
607 /* lctl gets a backstage, all-access pass. */
608 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
609 goto dont_check_exports;
611 spin_lock(&target->obd_dev_lock);
612 list_for_each(p, &target->obd_exports) {
613 export = list_entry(p, struct obd_export, exp_obd_chain);
614 if (obd_uuid_equals(&cluuid, &export->exp_client_uuid)) {
615 spin_unlock(&target->obd_dev_lock);
616 LASSERT(export->exp_obd == target);
618 rc = target_handle_reconnect(&conn, export, &cluuid);
623 /* If we found an export, we already unlocked. */
625 spin_unlock(&target->obd_dev_lock);
626 } else if (req->rq_reqmsg->conn_cnt == 1) {
627 CERROR("%s reconnected with 1 conn_cnt; cookies not random?\n",
629 GOTO(out, rc = -EALREADY);
632 /* Tell the client if we're in recovery. */
633 /* If this is the first client, start the recovery timer */
634 if (target->obd_recovering) {
635 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
636 target_start_recovery_timer(target, handler);
639 /* Tell the client if we support replayable requests */
640 if (target->obd_replayable)
641 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
643 if (export == NULL) {
644 if (target->obd_recovering) {
645 CERROR("denying connection for new client %s: "
646 "%d clients in recovery for %lds\n", cluuid.uuid,
647 target->obd_recoverable_clients,
648 (target->obd_recovery_timer.expires-jiffies)/HZ);
652 rc = obd_connect(&conn, target, &cluuid, data);
656 /* Return only the parts of obd_connect_data that we understand, so the
657 * client knows that we don't understand the rest. */
658 data->ocd_connect_flags &= OBD_CONNECT_SUPPORTED;
659 memcpy(lustre_msg_buf(req->rq_repmsg, 0, sizeof(*data)), data,
662 /* If all else goes well, this is our RPC return code. */
665 /* we want to handle EALREADY but *not* -EALREADY from
666 * target_handle_reconnect() */
667 if (rc && rc != EALREADY)
670 req->rq_repmsg->handle = conn;
672 /* If the client and the server are the same node, we will already
673 * have an export that really points to the client's DLM export,
674 * because we have a shared handles table.
676 * XXX this will go away when shaver stops sending the "connect" handle
677 * in the real "remote handle" field of the request --phik 24 Apr 2003
679 if (req->rq_export != NULL)
680 class_export_put(req->rq_export);
682 /* ownership of this export ref transfers to the request */
683 export = req->rq_export = class_conn2export(&conn);
684 LASSERT(export != NULL);
686 spin_lock_irqsave(&export->exp_lock, flags);
687 if (export->exp_conn_cnt >= req->rq_reqmsg->conn_cnt) {
688 CERROR("%s: already connected at a higher conn_cnt: %d > %d\n",
689 cluuid.uuid, export->exp_conn_cnt,
690 req->rq_reqmsg->conn_cnt);
691 spin_unlock_irqrestore(&export->exp_lock, flags);
692 GOTO(out, rc = -EALREADY);
694 export->exp_conn_cnt = req->rq_reqmsg->conn_cnt;
695 spin_unlock_irqrestore(&export->exp_lock, flags);
697 /* request from liblustre? */
698 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT)
699 export->exp_libclient = 1;
701 if (export->exp_connection != NULL)
702 ptlrpc_put_connection(export->exp_connection);
703 export->exp_connection = ptlrpc_get_connection(&req->rq_peer,
706 if (rc == EALREADY) {
707 /* We indicate the reconnection in a flag, not an error code. */
708 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
712 if (target->obd_recovering)
713 target->obd_connected_clients++;
715 memcpy(&conn, lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn),
718 if (export->exp_imp_reverse != NULL)
719 class_destroy_import(export->exp_imp_reverse);
720 revimp = export->exp_imp_reverse = class_new_import();
721 revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
722 revimp->imp_client = &export->exp_obd->obd_ldlm_client;
723 revimp->imp_remote_handle = conn;
724 revimp->imp_obd = target;
725 revimp->imp_dlm_fake = 1;
726 revimp->imp_state = LUSTRE_IMP_FULL;
727 class_import_put(revimp);
734 int target_handle_disconnect(struct ptlrpc_request *req)
736 struct obd_export *exp;
740 rc = lustre_pack_reply(req, 0, NULL, NULL);
744 /* keep the rq_export around so we can send the reply */
745 exp = class_export_get(req->rq_export);
746 req->rq_status = obd_disconnect(exp);
750 void target_destroy_export(struct obd_export *exp)
752 /* exports created from last_rcvd data, and "fake"
753 exports created by lctl don't have an import */
754 if (exp->exp_imp_reverse != NULL)
755 class_destroy_import(exp->exp_imp_reverse);
757 /* We cancel locks at disconnect time, but this will catch any locks
758 * granted in a race with recovery-induced disconnect. */
759 if (exp->exp_obd->obd_namespace != NULL)
760 ldlm_cancel_locks_for_export(exp);
768 static void target_release_saved_req(struct ptlrpc_request *req)
770 if (req->rq_reply_state != NULL) {
771 ptlrpc_rs_decref(req->rq_reply_state);
772 /* req->rq_reply_state = NULL; */
775 class_export_put(req->rq_export);
776 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
777 OBD_FREE(req, sizeof *req);
780 static void target_finish_recovery(struct obd_device *obd)
782 struct list_head *tmp, *n;
785 CWARN("%s: sending delayed replies to recovered clients\n",
788 ldlm_reprocess_all_ns(obd->obd_namespace);
790 /* when recovery finished, cleanup orphans on mds and ost */
791 if (OBT(obd) && OBP(obd, postrecov)) {
792 rc = OBP(obd, postrecov)(obd);
794 CWARN("%s: all clients recovered, %d MDS "
795 "orphans deleted\n", obd->obd_name, rc);
797 CERROR("postrecov failed %d\n", rc);
800 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
801 struct ptlrpc_request *req;
802 req = list_entry(tmp, struct ptlrpc_request, rq_list);
803 list_del(&req->rq_list);
804 DEBUG_REQ(D_WARNING, req, "delayed:");
806 target_release_saved_req(req);
808 obd->obd_recovery_end = CURRENT_SECONDS;
812 static void abort_recovery_queue(struct obd_device *obd)
814 struct ptlrpc_request *req;
815 struct list_head *tmp, *n;
818 list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
819 req = list_entry(tmp, struct ptlrpc_request, rq_list);
820 list_del(&req->rq_list);
821 DEBUG_REQ(D_ERROR, req, "aborted:");
822 req->rq_status = -ENOTCONN;
823 req->rq_type = PTL_RPC_MSG_ERR;
824 rc = lustre_pack_reply(req, 0, NULL, NULL);
828 DEBUG_REQ(D_ERROR, req,
829 "packing failed for abort-reply; skipping");
831 target_release_saved_req(req);
835 /* Called from a cleanup function if the device is being cleaned up
836 forcefully. The exports should all have been disconnected already,
837 the only thing left to do is
838 - clear the recovery flags
840 - free queued requests and replies, but don't send replies
841 Because the obd_stopping flag is set, no new requests should be received.
844 void target_cleanup_recovery(struct obd_device *obd)
846 struct list_head *tmp, *n;
847 struct ptlrpc_request *req;
849 LASSERT(obd->obd_stopping);
851 spin_lock_bh(&obd->obd_processing_task_lock);
852 if (!obd->obd_recovering) {
853 spin_unlock_bh(&obd->obd_processing_task_lock);
857 obd->obd_recovering = obd->obd_abort_recovery = 0;
858 target_cancel_recovery_timer(obd);
859 spin_unlock_bh(&obd->obd_processing_task_lock);
861 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
862 req = list_entry(tmp, struct ptlrpc_request, rq_list);
863 list_del(&req->rq_list);
864 target_release_saved_req(req);
867 list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
868 req = list_entry(tmp, struct ptlrpc_request, rq_list);
869 list_del(&req->rq_list);
870 target_release_saved_req(req);
874 void target_abort_recovery(void *data)
876 struct obd_device *obd = data;
878 spin_lock_bh(&obd->obd_processing_task_lock);
879 if (!obd->obd_recovering) {
880 spin_unlock_bh(&obd->obd_processing_task_lock);
884 obd->obd_recovering = obd->obd_abort_recovery = 0;
885 target_cancel_recovery_timer(obd);
886 spin_unlock_bh(&obd->obd_processing_task_lock);
888 CERROR("%s: recovery period over; disconnecting unfinished clients.\n",
890 class_disconnect_stale_exports(obd);
891 abort_recovery_queue(obd);
893 target_finish_recovery(obd);
895 ptlrpc_run_recovery_over_upcall(obd);
898 static void target_recovery_expired(unsigned long castmeharder)
900 struct obd_device *obd = (struct obd_device *)castmeharder;
901 CERROR("recovery timed out, aborting\n");
902 spin_lock_bh(&obd->obd_processing_task_lock);
903 if (obd->obd_recovering)
904 obd->obd_abort_recovery = 1;
905 wake_up(&obd->obd_next_transno_waitq);
906 spin_unlock_bh(&obd->obd_processing_task_lock);
910 /* obd_processing_task_lock should be held */
911 void target_cancel_recovery_timer(struct obd_device *obd)
913 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
914 del_timer(&obd->obd_recovery_timer);
917 static void reset_recovery_timer(struct obd_device *obd)
919 spin_lock_bh(&obd->obd_processing_task_lock);
920 if (!obd->obd_recovering) {
921 spin_unlock_bh(&obd->obd_processing_task_lock);
924 CDEBUG(D_HA, "%s: timer will expire in %u seconds\n", obd->obd_name,
925 (int)(OBD_RECOVERY_TIMEOUT / HZ));
926 mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
927 spin_unlock_bh(&obd->obd_processing_task_lock);
931 /* Only start it the first time called */
932 void target_start_recovery_timer(struct obd_device *obd, svc_handler_t handler)
934 spin_lock_bh(&obd->obd_processing_task_lock);
935 if (obd->obd_recovery_handler) {
936 spin_unlock_bh(&obd->obd_processing_task_lock);
939 CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
940 (int)(OBD_RECOVERY_TIMEOUT / HZ));
941 obd->obd_recovery_handler = handler;
942 obd->obd_recovery_timer.function = target_recovery_expired;
943 obd->obd_recovery_timer.data = (unsigned long)obd;
944 spin_unlock_bh(&obd->obd_processing_task_lock);
946 reset_recovery_timer(obd);
949 static int check_for_next_transno(struct obd_device *obd)
951 struct ptlrpc_request *req;
952 int wake_up = 0, connected, completed, queue_len, max;
953 __u64 next_transno, req_transno;
955 spin_lock_bh(&obd->obd_processing_task_lock);
956 req = list_entry(obd->obd_recovery_queue.next,
957 struct ptlrpc_request, rq_list);
958 max = obd->obd_max_recoverable_clients;
959 req_transno = req->rq_reqmsg->transno;
960 connected = obd->obd_connected_clients;
961 completed = max - obd->obd_recoverable_clients;
962 queue_len = obd->obd_requests_queued_for_recovery;
963 next_transno = obd->obd_next_recovery_transno;
965 CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
966 "req_transno: "LPU64", next_transno: "LPU64"\n",
967 max, connected, completed, queue_len, req_transno, next_transno);
968 if (obd->obd_abort_recovery) {
969 CDEBUG(D_HA, "waking for aborted recovery\n");
971 } else if (!obd->obd_recovering) {
972 CDEBUG(D_HA, "waking for completed recovery (?)\n");
974 } else if (req_transno == next_transno) {
975 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
977 } else if (queue_len + completed == max) {
979 "waking for skipped transno (skip: "LPD64
980 ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
981 next_transno, queue_len, completed, max, req_transno);
982 obd->obd_next_recovery_transno = req_transno;
985 spin_unlock_bh(&obd->obd_processing_task_lock);
986 LASSERT(req->rq_reqmsg->transno >= next_transno);
990 static void process_recovery_queue(struct obd_device *obd)
992 struct ptlrpc_request *req;
993 int abort_recovery = 0;
994 struct l_wait_info lwi = { 0 };
998 spin_lock_bh(&obd->obd_processing_task_lock);
999 LASSERT(obd->obd_processing_task == current->pid);
1000 req = list_entry(obd->obd_recovery_queue.next,
1001 struct ptlrpc_request, rq_list);
1003 if (req->rq_reqmsg->transno != obd->obd_next_recovery_transno) {
1004 spin_unlock_bh(&obd->obd_processing_task_lock);
1005 CDEBUG(D_HA, "Waiting for transno "LPD64" (1st is "
1007 obd->obd_next_recovery_transno,
1008 req->rq_reqmsg->transno);
1009 l_wait_event(obd->obd_next_transno_waitq,
1010 check_for_next_transno(obd), &lwi);
1011 spin_lock_bh(&obd->obd_processing_task_lock);
1012 abort_recovery = obd->obd_abort_recovery;
1013 spin_unlock_bh(&obd->obd_processing_task_lock);
1014 if (abort_recovery) {
1015 target_abort_recovery(obd);
1020 list_del_init(&req->rq_list);
1021 obd->obd_requests_queued_for_recovery--;
1022 spin_unlock_bh(&obd->obd_processing_task_lock);
1024 DEBUG_REQ(D_HA, req, "processing: ");
1025 (void)obd->obd_recovery_handler(req);
1026 obd->obd_replayed_requests++;
1027 reset_recovery_timer(obd);
1028 /* bug 1580: decide how to properly sync() in recovery */
1029 //mds_fsync_super(mds->mds_sb);
1030 class_export_put(req->rq_export);
1031 if (req->rq_reply_state != NULL) {
1032 ptlrpc_rs_decref(req->rq_reply_state);
1033 /* req->rq_reply_state = NULL; */
1035 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
1036 OBD_FREE(req, sizeof *req);
1037 spin_lock_bh(&obd->obd_processing_task_lock);
1038 obd->obd_next_recovery_transno++;
1039 if (list_empty(&obd->obd_recovery_queue)) {
1040 obd->obd_processing_task = 0;
1041 spin_unlock_bh(&obd->obd_processing_task_lock);
1044 spin_unlock_bh(&obd->obd_processing_task_lock);
1049 int target_queue_recovery_request(struct ptlrpc_request *req,
1050 struct obd_device *obd)
1052 struct list_head *tmp;
1054 __u64 transno = req->rq_reqmsg->transno;
1055 struct ptlrpc_request *saved_req;
1056 struct lustre_msg *reqmsg;
1058 /* CAVEAT EMPTOR: The incoming request message has been swabbed
1059 * (i.e. buflens etc are in my own byte order), but type-dependent
1060 * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1063 INIT_LIST_HEAD(&req->rq_list);
1064 DEBUG_REQ(D_HA, req, "not queueing");
1068 /* XXX If I were a real man, these LBUGs would be sane cleanups. */
1069 /* XXX just like the request-dup code in queue_final_reply */
1070 OBD_ALLOC(saved_req, sizeof *saved_req);
1073 OBD_ALLOC(reqmsg, req->rq_reqlen);
1077 spin_lock_bh(&obd->obd_processing_task_lock);
1079 /* If we're processing the queue, we want don't want to queue this
1082 * Also, if this request has a transno less than the one we're waiting
1083 * for, we should process it now. It could (and currently always will)
1084 * be an open request for a descriptor that was opened some time ago.
1086 * Also, a resent, replayed request that has already been
1087 * handled will pass through here and be processed immediately.
1089 if (obd->obd_processing_task == current->pid ||
1090 transno < obd->obd_next_recovery_transno) {
1091 /* Processing the queue right now, don't re-add. */
1092 LASSERT(list_empty(&req->rq_list));
1093 spin_unlock_bh(&obd->obd_processing_task_lock);
1094 OBD_FREE(reqmsg, req->rq_reqlen);
1095 OBD_FREE(saved_req, sizeof *saved_req);
1099 /* A resent, replayed request that is still on the queue; just drop it.
1100 The queued request will handle this. */
1101 if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT|MSG_REPLAY)) ==
1102 (MSG_RESENT | MSG_REPLAY)) {
1103 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1104 spin_unlock_bh(&obd->obd_processing_task_lock);
1105 OBD_FREE(reqmsg, req->rq_reqlen);
1106 OBD_FREE(saved_req, sizeof *saved_req);
1110 memcpy(saved_req, req, sizeof *req);
1111 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1113 req->rq_reqmsg = reqmsg;
1114 class_export_get(req->rq_export);
1115 INIT_LIST_HEAD(&req->rq_list);
1118 list_for_each(tmp, &obd->obd_recovery_queue) {
1119 struct ptlrpc_request *reqiter =
1120 list_entry(tmp, struct ptlrpc_request, rq_list);
1122 if (reqiter->rq_reqmsg->transno > transno) {
1123 list_add_tail(&req->rq_list, &reqiter->rq_list);
1130 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
1133 obd->obd_requests_queued_for_recovery++;
1135 if (obd->obd_processing_task != 0) {
1136 /* Someone else is processing this queue, we'll leave it to
1139 wake_up(&obd->obd_next_transno_waitq);
1140 spin_unlock_bh(&obd->obd_processing_task_lock);
1144 /* Nobody is processing, and we know there's (at least) one to process
1145 * now, so we'll do the honours.
1147 obd->obd_processing_task = current->pid;
1148 spin_unlock_bh(&obd->obd_processing_task_lock);
1150 process_recovery_queue(obd);
1154 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1156 return req->rq_export->exp_obd;
1159 int target_queue_final_reply(struct ptlrpc_request *req, int rc)
1161 struct obd_device *obd = target_req2obd(req);
1162 struct ptlrpc_request *saved_req;
1163 struct lustre_msg *reqmsg;
1164 int recovery_done = 0;
1166 LASSERT ((rc == 0) == (req->rq_reply_state != NULL));
1169 /* Just like ptlrpc_error, but without the sending. */
1170 rc = lustre_pack_reply(req, 0, NULL, NULL);
1171 LASSERT(rc == 0); /* XXX handle this */
1172 req->rq_type = PTL_RPC_MSG_ERR;
1175 LASSERT (!req->rq_reply_state->rs_difficult);
1176 LASSERT(list_empty(&req->rq_list));
1177 /* XXX a bit like the request-dup code in queue_recovery_request */
1178 OBD_ALLOC(saved_req, sizeof *saved_req);
1181 OBD_ALLOC(reqmsg, req->rq_reqlen);
1184 memcpy(saved_req, req, sizeof *saved_req);
1185 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1187 /* Don't race cleanup */
1188 spin_lock_bh(&obd->obd_processing_task_lock);
1189 if (obd->obd_stopping) {
1190 spin_unlock_bh(&obd->obd_processing_task_lock);
1191 OBD_FREE(reqmsg, req->rq_reqlen);
1192 OBD_FREE(saved_req, sizeof *req);
1193 req->rq_status = -ENOTCONN;
1194 /* rv is ignored anyhow */
1197 ptlrpc_rs_addref(req->rq_reply_state); /* +1 ref for saved reply */
1199 req->rq_reqmsg = reqmsg;
1200 class_export_get(req->rq_export);
1201 list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
1203 /* only count the first "replay over" request from each
1205 if (req->rq_export->exp_replay_needed) {
1206 --obd->obd_recoverable_clients;
1207 req->rq_export->exp_replay_needed = 0;
1209 recovery_done = (obd->obd_recoverable_clients == 0);
1210 spin_unlock_bh(&obd->obd_processing_task_lock);
1212 OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
1213 if (recovery_done) {
1214 spin_lock_bh(&obd->obd_processing_task_lock);
1215 obd->obd_recovering = obd->obd_abort_recovery = 0;
1216 target_cancel_recovery_timer(obd);
1217 spin_unlock_bh(&obd->obd_processing_task_lock);
1219 target_finish_recovery(obd);
1220 ptlrpc_run_recovery_over_upcall(obd);
1222 CWARN("%s: %d recoverable clients remain\n",
1223 obd->obd_name, obd->obd_recoverable_clients);
1224 wake_up(&obd->obd_next_transno_waitq);
1231 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1233 if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1234 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1235 DEBUG_REQ(D_ERROR, req, "dropping reply");
1240 DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
1241 req->rq_status = rc;
1242 return (ptlrpc_error(req));
1244 DEBUG_REQ(D_NET, req, "sending reply");
1247 return (ptlrpc_send_reply(req, 1));
1251 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1254 unsigned long flags;
1255 struct ptlrpc_reply_state *rs;
1256 struct obd_device *obd;
1257 struct obd_export *exp;
1258 struct ptlrpc_srv_ni *sni;
1259 struct ptlrpc_service *svc;
1261 sni = req->rq_rqbd->rqbd_srv_ni;
1262 svc = sni->sni_service;
1264 rs = req->rq_reply_state;
1265 if (rs == NULL || !rs->rs_difficult) {
1267 target_send_reply_msg (req, rc, fail_id);
1271 /* must be an export if locks saved */
1272 LASSERT (req->rq_export != NULL);
1273 /* req/reply consistent */
1274 LASSERT (rs->rs_srv_ni == sni);
1277 LASSERT (!rs->rs_scheduled);
1278 LASSERT (!rs->rs_scheduled_ever);
1279 LASSERT (!rs->rs_handled);
1280 LASSERT (!rs->rs_on_net);
1281 LASSERT (rs->rs_export == NULL);
1282 LASSERT (list_empty(&rs->rs_obd_list));
1283 LASSERT (list_empty(&rs->rs_exp_list));
1285 exp = class_export_get (req->rq_export);
1288 /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1289 rs->rs_scheduled = 1;
1291 rs->rs_xid = req->rq_xid;
1292 rs->rs_transno = req->rq_transno;
1293 rs->rs_export = exp;
1295 spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
1297 if (rs->rs_transno > obd->obd_last_committed) {
1298 /* not committed already */
1299 list_add_tail (&rs->rs_obd_list,
1300 &obd->obd_uncommitted_replies);
1303 spin_unlock (&obd->obd_uncommitted_replies_lock);
1304 spin_lock (&exp->exp_lock);
1306 list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1308 spin_unlock_irqrestore (&exp->exp_lock, flags);
1310 netrc = target_send_reply_msg (req, rc, fail_id);
1312 spin_lock_irqsave (&svc->srv_lock, flags);
1314 svc->srv_n_difficult_replies++;
1317 /* error sending: reply is off the net. Also we need +1
1318 * reply ref until ptlrpc_server_handle_reply() is done
1319 * with the reply state (if the send was successful, there
1320 * would have been +1 ref for the net, which
1321 * reply_out_callback leaves alone) */
1323 ptlrpc_rs_addref(rs);
1324 atomic_inc (&svc->srv_outstanding_replies);
1327 if (!rs->rs_on_net || /* some notifier */
1328 list_empty(&rs->rs_exp_list) || /* completed already */
1329 list_empty(&rs->rs_obd_list)) {
1330 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1331 wake_up (&svc->srv_waitq);
1333 list_add (&rs->rs_list, &sni->sni_active_replies);
1334 rs->rs_scheduled = 0; /* allow notifier to schedule */
1337 spin_unlock_irqrestore (&svc->srv_lock, flags);
1340 int target_handle_ping(struct ptlrpc_request *req)
1342 return lustre_pack_reply(req, 0, NULL, NULL);
1345 void target_committed_to_req(struct ptlrpc_request *req)
1347 struct obd_device *obd = req->rq_export->exp_obd;
1349 if (!obd->obd_no_transno && req->rq_repmsg != NULL)
1350 req->rq_repmsg->last_committed = obd->obd_last_committed;
1352 DEBUG_REQ(D_IOCTL, req,
1353 "not sending last_committed update");
1355 CDEBUG(D_INFO, "last_committed "LPU64", xid "LPU64"\n",
1356 obd->obd_last_committed, req->rq_xid);
1359 int target_handle_qc_callback(struct ptlrpc_request *req)
1361 struct obd_quotactl *oqctl;
1362 struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
1364 oqctl = lustre_swab_reqbuf(req, 0, sizeof(*oqctl),
1365 lustre_swab_obd_quotactl);
1367 spin_lock(&cli->cl_qchk_lock);
1368 cli->cl_qchk_stat = oqctl->qc_stat;
1369 spin_unlock(&cli->cl_qchk_lock);
1374 int target_handle_dqacq_callback(struct ptlrpc_request *req)
1376 struct obd_device *obd = req->rq_export->exp_obd;
1377 struct obd_device *master_obd;
1378 struct lustre_quota_ctxt *qctxt;
1379 struct qunit_data *qdata, *rep;
1380 int rc = 0, repsize = sizeof(struct qunit_data);
1383 rc = lustre_pack_reply(req, 1, &repsize, NULL);
1385 CERROR("packing reply failed!: rc = %d\n", rc);
1388 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*rep));
1391 qdata = lustre_swab_reqbuf(req, 0, sizeof(*qdata), lustre_swab_qdata);
1392 if (qdata == NULL) {
1393 CERROR("unpacking request buffer failed!");
1397 /* we use the observer */
1398 LASSERT(obd->obd_observer && obd->obd_observer->obd_observer);
1399 master_obd = obd->obd_observer->obd_observer;
1400 qctxt = &master_obd->u.mds.mds_quota_ctxt;
1402 LASSERT(qctxt->lqc_handler);
1403 rc = qctxt->lqc_handler(master_obd, qdata, req->rq_reqmsg->opc);
1404 if (rc && rc != -EDQUOT)
1405 CERROR("dqacq failed! (rc:%d)\n", rc);
1407 /* the qd_count might be changed in lqc_handler */
1408 memcpy(rep, qdata, sizeof(*rep));
1409 req->rq_status = rc;
1410 rc = ptlrpc_reply(req);
1415 EXPORT_SYMBOL(target_committed_to_req);