1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2003 Cluster File Systems, Inc.
6 * This file is part of the Lustre file system, http://www.lustre.org
7 * Lustre is a trademark of Cluster File Systems, Inc.
9 * You may have signed or agreed to another license before downloading
10 * this software. If so, you are bound by the terms and conditions
11 * of that agreement, and the following does not apply to you. See the
12 * LICENSE file included with this distribution for more information.
14 * If you did not agree to a different license, then this copy of Lustre
15 * is open source software; you can redistribute it and/or modify it
16 * under the terms of version 2 of the GNU General Public License as
17 * published by the Free Software Foundation.
19 * In either case, Lustre is distributed in the hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * license text for more details.
26 # define EXPORT_SYMTAB
28 #define DEBUG_SUBSYSTEM S_LDLM
31 # include <linux/module.h>
33 # include <liblustre.h>
35 #include <linux/obd.h>
36 #include <linux/obd_ost.h> /* for LUSTRE_OSC_NAME */
37 #include <linux/lustre_mds.h> /* for LUSTRE_MDC_NAME */
38 #include <linux/lustre_dlm.h>
39 #include <linux/lustre_net.h>
41 /* @priority: if non-zero, move the selected to the list head
42 * @create: if zero, only search in existed connections
44 static int import_set_conn(struct obd_import *imp, struct obd_uuid *uuid,
45 int priority, int create)
47 struct ptlrpc_connection *ptlrpc_conn;
48 struct obd_import_conn *imp_conn = NULL, *item;
52 if (!create && !priority) {
53 CDEBUG(D_HA, "Nothing to do\n");
57 ptlrpc_conn = ptlrpc_uuid_to_connection(uuid);
59 CDEBUG(D_HA, "can't find connection %s\n", uuid->uuid);
64 OBD_ALLOC(imp_conn, sizeof(*imp_conn));
66 GOTO(out_put, rc = -ENOMEM);
70 spin_lock(&imp->imp_lock);
71 list_for_each_entry(item, &imp->imp_conn_list, oic_item) {
72 if (obd_uuid_equals(uuid, &item->oic_uuid)) {
74 list_del(&item->oic_item);
75 list_add(&item->oic_item, &imp->imp_conn_list);
77 CDEBUG(D_HA, "imp %p@%s: found existing conn %s%s\n",
78 imp, imp->imp_obd->obd_name, uuid->uuid,
79 (priority ? ", moved to head" : ""));
80 spin_unlock(&imp->imp_lock);
81 GOTO(out_free, rc = 0);
86 imp_conn->oic_conn = ptlrpc_conn;
87 imp_conn->oic_uuid = *uuid;
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_import_conn *cur_conn;
126 struct obd_export *dlmexp;
130 spin_lock(&imp->imp_lock);
131 if (list_empty(&imp->imp_conn_list)) {
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 cur_conn = list_entry(imp->imp_conn_list.next,
142 struct obd_import_conn,
145 /* is current conn? */
146 if (imp_conn == cur_conn) {
147 LASSERT(imp_conn->oic_conn == imp->imp_connection);
149 if (imp->imp_state != LUSTRE_IMP_CLOSED &&
150 imp->imp_state != LUSTRE_IMP_DISCON) {
151 CERROR("can't remove current connection\n");
152 GOTO(out, rc = -EBUSY);
155 ptlrpc_put_connection(imp->imp_connection);
156 imp->imp_connection = NULL;
158 dlmexp = class_conn2export(&imp->imp_dlm_handle);
159 if (dlmexp && dlmexp->exp_connection) {
160 LASSERT(dlmexp->exp_connection ==
162 ptlrpc_put_connection(dlmexp->exp_connection);
163 dlmexp->exp_connection = NULL;
167 list_del(&imp_conn->oic_item);
168 ptlrpc_put_connection(imp_conn->oic_conn);
169 OBD_FREE(imp_conn, sizeof(*imp_conn));
170 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
171 imp, imp->imp_obd->obd_name, uuid->uuid);
176 spin_unlock(&imp->imp_lock);
178 CERROR("connection %s not found\n", uuid->uuid);
182 /* configure an RPC client OBD device
187 * 3 - inactive-on-startup
189 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
191 struct lustre_cfg* lcfg = buf;
192 struct client_obd *cli = &obddev->u.cli;
193 struct obd_import *imp;
194 struct obd_uuid server_uuid;
195 int rq_portal, rp_portal, connect_op;
196 char *name = obddev->obd_type->typ_name;
200 /* In a more perfect world, we would hang a ptlrpc_client off of
201 * obd_type and just use the values from there. */
202 if (!strcmp(name, LUSTRE_OSC_NAME)) {
203 rq_portal = OST_REQUEST_PORTAL;
204 rp_portal = OSC_REPLY_PORTAL;
205 connect_op = OST_CONNECT;
206 } else if (!strcmp(name, LUSTRE_MDC_NAME)) {
207 rq_portal = MDS_REQUEST_PORTAL;
208 rp_portal = MDC_REPLY_PORTAL;
209 connect_op = MDS_CONNECT;
211 CERROR("unknown client OBD type \"%s\", can't setup\n",
216 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
217 CERROR("requires a TARGET UUID\n");
221 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
222 CERROR("client UUID must be less than 38 characters\n");
226 if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
227 CERROR("setup requires a SERVER UUID\n");
231 if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
232 CERROR("target UUID must be less than 38 characters\n");
236 sema_init(&cli->cl_sem, 1);
237 cli->cl_conn_count = 0;
238 memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
239 min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
240 sizeof(server_uuid)));
243 cli->cl_avail_grant = 0;
244 /* FIXME: should limit this for the sum of all cl_dirty_max */
245 cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
246 if (cli->cl_dirty_max >> PAGE_SHIFT > num_physpages / 8)
247 cli->cl_dirty_max = num_physpages << (PAGE_SHIFT - 3);
248 INIT_LIST_HEAD(&cli->cl_cache_waiters);
249 INIT_LIST_HEAD(&cli->cl_loi_ready_list);
250 INIT_LIST_HEAD(&cli->cl_loi_write_list);
251 INIT_LIST_HEAD(&cli->cl_loi_read_list);
252 spin_lock_init(&cli->cl_loi_list_lock);
253 cli->cl_r_in_flight = 0;
254 cli->cl_w_in_flight = 0;
255 spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
256 spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
257 spin_lock_init(&cli->cl_read_page_hist.oh_lock);
258 spin_lock_init(&cli->cl_write_page_hist.oh_lock);
259 spin_lock_init(&cli->cl_read_offset_hist.oh_lock);
260 spin_lock_init(&cli->cl_write_offset_hist.oh_lock);
261 if (num_physpages >> (20 - PAGE_SHIFT) <= 128) { /* <= 128 MB */
262 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 4;
263 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 4;
264 } else if (num_physpages >> (20 - PAGE_SHIFT) <= 512) { /* <= 512 MB */
265 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 2;
266 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 2;
268 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES;
269 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
274 CERROR("ldlm_get_ref failed: %d\n", rc);
278 ptlrpc_init_client(rq_portal, rp_portal, name,
279 &obddev->obd_ldlm_client);
281 imp = class_new_import();
283 GOTO(err_ldlm, rc = -ENOENT);
284 imp->imp_client = &obddev->obd_ldlm_client;
285 imp->imp_obd = obddev;
286 imp->imp_connect_op = connect_op;
287 imp->imp_generation = 0;
288 imp->imp_initial_recov = 1;
289 INIT_LIST_HEAD(&imp->imp_pinger_chain);
290 memcpy(imp->imp_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
291 LUSTRE_CFG_BUFLEN(lcfg, 1));
292 class_import_put(imp);
294 rc = client_import_add_conn(imp, &server_uuid, 1);
296 CERROR("can't add initial connection\n");
297 GOTO(err_import, rc);
300 cli->cl_import = imp;
301 /* cli->cl_max_mds_{easize,cookiesize} updated by mdc_init_ea_size() */
302 cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
303 cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
304 cli->cl_sandev = to_kdev_t(0);
306 if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
307 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
308 CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
309 name, obddev->obd_name,
310 imp->imp_target_uuid.uuid);
311 imp->imp_invalid = 1;
315 spin_lock_init(&cli->cl_qchk_lock);
316 cli->cl_qchk_stat = CL_NO_QUOTACHECK;
321 class_destroy_import(imp);
329 int client_obd_cleanup(struct obd_device *obddev)
331 struct client_obd *cli = &obddev->u.cli;
335 class_destroy_import(cli->cl_import);
336 cli->cl_import = NULL;
338 ldlm_put_ref(obddev->obd_force);
343 /* ->o_connect() method for client side (OSC and MDC) */
344 int client_connect_import(struct lustre_handle *dlm_handle,
345 struct obd_device *obd, struct obd_uuid *cluuid,
346 struct obd_connect_data *data)
348 struct client_obd *cli = &obd->u.cli;
349 struct obd_import *imp = cli->cl_import;
350 struct obd_export *exp;
351 struct obd_connect_data *ocd;
356 rc = class_connect(dlm_handle, obd, cluuid);
360 cli->cl_conn_count++;
361 if (cli->cl_conn_count > 1)
363 exp = class_conn2export(dlm_handle);
365 if (obd->obd_namespace != NULL)
366 CERROR("already have namespace!\n");
367 obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
368 LDLM_NAMESPACE_CLIENT);
369 if (obd->obd_namespace == NULL)
370 GOTO(out_disco, rc = -ENOMEM);
372 imp->imp_dlm_handle = *dlm_handle;
373 rc = ptlrpc_init_import(imp);
377 ocd = &imp->imp_connect_data;
381 rc = ptlrpc_connect_import(imp, NULL);
383 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
386 LASSERT(exp->exp_connection);
389 LASSERT((ocd->ocd_connect_flags & data->ocd_connect_flags) ==
390 ocd->ocd_connect_flags);
391 data->ocd_connect_flags = ocd->ocd_connect_flags;
394 ptlrpc_pinger_add_import(imp);
399 ldlm_namespace_free(obd->obd_namespace, 0);
400 obd->obd_namespace = NULL;
402 cli->cl_conn_count--;
403 class_disconnect(exp);
405 class_export_put(exp);
412 int client_disconnect_export(struct obd_export *exp)
414 struct obd_device *obd = class_exp2obd(exp);
415 struct client_obd *cli = &obd->u.cli;
416 struct obd_import *imp = cli->cl_import;
421 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
422 exp, exp ? exp->exp_handle.h_cookie : -1);
427 if (!cli->cl_conn_count) {
428 CERROR("disconnecting disconnected device (%s)\n",
430 GOTO(out_sem, rc = -EINVAL);
433 cli->cl_conn_count--;
434 if (cli->cl_conn_count)
435 GOTO(out_no_disconnect, rc = 0);
437 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
438 * delete it regardless. (It's safe to delete an import that was
440 (void)ptlrpc_pinger_del_import(imp);
442 if (obd->obd_namespace != NULL) {
443 /* obd_no_recov == local only */
444 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
445 obd->obd_no_recov, NULL);
446 ldlm_namespace_free(obd->obd_namespace, obd->obd_no_recov);
447 obd->obd_namespace = NULL;
450 /* Yeah, obd_no_recov also (mainly) means "forced shutdown". */
451 if (obd->obd_no_recov)
452 ptlrpc_invalidate_import(imp);
454 rc = ptlrpc_disconnect_import(imp);
458 err = class_disconnect(exp);
466 /* --------------------------------------------------------------------------
467 * from old lib/target.c
468 * -------------------------------------------------------------------------- */
470 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
471 struct obd_uuid *cluuid)
473 if (exp->exp_connection) {
474 struct lustre_handle *hdl;
475 hdl = &exp->exp_imp_reverse->imp_remote_handle;
476 /* Might be a re-connect after a partition. */
477 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
478 CWARN("%s reconnecting\n", cluuid->uuid);
479 conn->cookie = exp->exp_handle.h_cookie;
480 /* target_handle_connect() treats EALREADY and
481 * -EALREADY differently */
484 CERROR("%s reconnecting from %s, "
485 "handle mismatch (ours "LPX64", theirs "
486 LPX64")\n", cluuid->uuid,
487 exp->exp_connection->c_remote_uuid.uuid,
488 hdl->cookie, conn->cookie);
489 memset(conn, 0, sizeof *conn);
490 /* target_handle_connect() treats EALREADY and
491 * -EALREADY differently */
496 conn->cookie = exp->exp_handle.h_cookie;
497 CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
499 CDEBUG(D_IOCTL, "connect: cookie "LPX64"\n", conn->cookie);
503 int target_handle_connect(struct ptlrpc_request *req, svc_handler_t handler)
505 struct obd_device *target;
506 struct obd_export *export = NULL;
507 struct obd_import *revimp;
508 struct lustre_handle conn;
509 struct obd_uuid tgtuuid;
510 struct obd_uuid cluuid;
511 struct obd_uuid remote_uuid;
514 int rc = 0, abort_recovery;
516 struct obd_connect_data *data;
517 int size = sizeof(*data);
520 OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
522 LASSERT_REQSWAB (req, 0);
523 str = lustre_msg_string(req->rq_reqmsg, 0, sizeof(tgtuuid) - 1);
525 DEBUG_REQ(D_ERROR, req, "bad target UUID for connect");
526 GOTO(out, rc = -EINVAL);
529 obd_str2uuid (&tgtuuid, str);
530 target = class_uuid2obd(&tgtuuid);
532 target = class_name2obd(str);
535 if (!target || target->obd_stopping || !target->obd_set_up) {
536 DEBUG_REQ(D_ERROR, req, "UUID '%s' is not available "
537 " for connect (%s)", str,
538 !target ? "no target" :
539 (target->obd_stopping ? "stopping" : "not set up"));
540 GOTO(out, rc = -ENODEV);
543 LASSERT_REQSWAB (req, 1);
544 str = lustre_msg_string(req->rq_reqmsg, 1, sizeof(cluuid) - 1);
546 DEBUG_REQ(D_ERROR, req, "bad client UUID for connect");
547 GOTO(out, rc = -EINVAL);
550 obd_str2uuid (&cluuid, str);
552 /* XXX extract a nettype and format accordingly */
553 switch (sizeof(lnet_nid_t)) {
554 /* NB the casts only avoid compiler warnings */
556 snprintf(remote_uuid.uuid, sizeof remote_uuid,
557 "NET_"LPX64"_UUID", (__u64)req->rq_peer.nid);
560 snprintf(remote_uuid.uuid, sizeof remote_uuid,
561 "NET_%x_UUID", (__u32)req->rq_peer.nid);
567 spin_lock_bh(&target->obd_processing_task_lock);
568 abort_recovery = target->obd_abort_recovery;
569 spin_unlock_bh(&target->obd_processing_task_lock);
571 target_abort_recovery(target);
573 tmp = lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn);
575 GOTO(out, rc = -EPROTO);
577 memcpy(&conn, tmp, sizeof conn);
579 data = lustre_swab_reqbuf(req, 3, sizeof(*data), lustre_swab_connect);
580 rc = lustre_pack_reply(req, 1, &size, NULL);
584 /* lctl gets a backstage, all-access pass. */
585 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
586 goto dont_check_exports;
588 spin_lock(&target->obd_dev_lock);
589 list_for_each(p, &target->obd_exports) {
590 export = list_entry(p, struct obd_export, exp_obd_chain);
591 if (obd_uuid_equals(&cluuid, &export->exp_client_uuid)) {
592 spin_unlock(&target->obd_dev_lock);
593 LASSERT(export->exp_obd == target);
595 rc = target_handle_reconnect(&conn, export, &cluuid);
600 /* If we found an export, we already unlocked. */
602 spin_unlock(&target->obd_dev_lock);
603 } else if (req->rq_reqmsg->conn_cnt == 1) {
604 CERROR("%s reconnected with 1 conn_cnt; cookies not random?\n",
606 GOTO(out, rc = -EALREADY);
609 /* Tell the client if we're in recovery. */
610 /* If this is the first client, start the recovery timer */
611 if (target->obd_recovering) {
612 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
613 target_start_recovery_timer(target, handler);
616 /* Tell the client if we support replayable requests */
617 if (target->obd_replayable)
618 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
620 if (export == NULL) {
621 if (target->obd_recovering) {
622 CERROR("%s: denying connection for new client %s: "
623 "%d clients in recovery for %lds\n",
624 target->obd_name, cluuid.uuid,
625 target->obd_recoverable_clients,
626 (target->obd_recovery_timer.expires-jiffies)/HZ);
630 rc = obd_connect(&conn, target, &cluuid, data);
634 /* Return only the parts of obd_connect_data that we understand, so the
635 * client knows that we don't understand the rest. */
637 memcpy(lustre_msg_buf(req->rq_repmsg, 0, sizeof(*data)), data,
640 /* If all else goes well, this is our RPC return code. */
643 /* we want to handle EALREADY but *not* -EALREADY from
644 * target_handle_reconnect() */
645 if (rc && rc != EALREADY)
648 req->rq_repmsg->handle = conn;
650 /* If the client and the server are the same node, we will already
651 * have an export that really points to the client's DLM export,
652 * because we have a shared handles table.
654 * XXX this will go away when shaver stops sending the "connect" handle
655 * in the real "remote handle" field of the request --phik 24 Apr 2003
657 if (req->rq_export != NULL)
658 class_export_put(req->rq_export);
660 /* ownership of this export ref transfers to the request */
661 export = req->rq_export = class_conn2export(&conn);
662 LASSERT(export != NULL);
664 spin_lock_irqsave(&export->exp_lock, flags);
665 if (export->exp_conn_cnt >= req->rq_reqmsg->conn_cnt) {
666 CERROR("%s: already connected at a higher conn_cnt: %d > %d\n",
667 cluuid.uuid, export->exp_conn_cnt,
668 req->rq_reqmsg->conn_cnt);
669 spin_unlock_irqrestore(&export->exp_lock, flags);
670 GOTO(out, rc = -EALREADY);
672 export->exp_conn_cnt = req->rq_reqmsg->conn_cnt;
673 spin_unlock_irqrestore(&export->exp_lock, flags);
675 /* request from liblustre? Don't evict it for not pinging. */
676 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
677 export->exp_libclient = 1;
678 spin_lock(&target->obd_dev_lock);
679 list_del_init(&export->exp_obd_chain_timed);
680 spin_unlock(&target->obd_dev_lock);
683 if (export->exp_connection != NULL)
684 ptlrpc_put_connection(export->exp_connection);
685 export->exp_connection = ptlrpc_get_connection(req->rq_peer,
688 if (rc == EALREADY) {
689 /* We indicate the reconnection in a flag, not an error code. */
690 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
694 if (target->obd_recovering)
695 target->obd_connected_clients++;
697 memcpy(&conn, lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn),
700 if (export->exp_imp_reverse != NULL)
701 class_destroy_import(export->exp_imp_reverse);
702 revimp = export->exp_imp_reverse = class_new_import();
703 revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
704 revimp->imp_client = &export->exp_obd->obd_ldlm_client;
705 revimp->imp_remote_handle = conn;
706 revimp->imp_obd = target;
707 revimp->imp_dlm_fake = 1;
708 revimp->imp_state = LUSTRE_IMP_FULL;
709 class_import_put(revimp);
716 int target_handle_disconnect(struct ptlrpc_request *req)
718 struct obd_export *exp;
722 rc = lustre_pack_reply(req, 0, NULL, NULL);
726 /* keep the rq_export around so we can send the reply */
727 exp = class_export_get(req->rq_export);
728 req->rq_status = obd_disconnect(exp);
732 void target_destroy_export(struct obd_export *exp)
734 /* exports created from last_rcvd data, and "fake"
735 exports created by lctl don't have an import */
736 if (exp->exp_imp_reverse != NULL)
737 class_destroy_import(exp->exp_imp_reverse);
739 /* We cancel locks at disconnect time, but this will catch any locks
740 * granted in a race with recovery-induced disconnect. */
741 if (exp->exp_obd->obd_namespace != NULL)
742 ldlm_cancel_locks_for_export(exp);
750 static void target_release_saved_req(struct ptlrpc_request *req)
752 if (req->rq_reply_state != NULL) {
753 ptlrpc_rs_decref(req->rq_reply_state);
754 /* req->rq_reply_state = NULL; */
757 class_export_put(req->rq_export);
758 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
759 OBD_FREE(req, sizeof *req);
762 static void target_finish_recovery(struct obd_device *obd)
764 struct list_head *tmp, *n;
767 CWARN("%s: sending delayed replies to recovered clients\n",
770 ldlm_reprocess_all_ns(obd->obd_namespace);
772 /* when recovery finished, cleanup orphans on mds and ost */
773 if (OBT(obd) && OBP(obd, postrecov)) {
774 rc = OBP(obd, postrecov)(obd);
776 CWARN("%s: all clients recovered, %d MDS "
777 "orphans deleted\n", obd->obd_name, rc);
779 CERROR("postrecov failed %d\n", rc);
782 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
783 struct ptlrpc_request *req;
784 req = list_entry(tmp, struct ptlrpc_request, rq_list);
785 list_del(&req->rq_list);
786 DEBUG_REQ(D_WARNING, req, "delayed:");
788 target_release_saved_req(req);
790 obd->obd_recovery_end = CURRENT_SECONDS;
794 static void abort_recovery_queue(struct obd_device *obd)
796 struct ptlrpc_request *req;
797 struct list_head *tmp, *n;
800 list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
801 req = list_entry(tmp, struct ptlrpc_request, rq_list);
802 list_del(&req->rq_list);
803 DEBUG_REQ(D_ERROR, req, "aborted:");
804 req->rq_status = -ENOTCONN;
805 req->rq_type = PTL_RPC_MSG_ERR;
806 rc = lustre_pack_reply(req, 0, NULL, NULL);
810 DEBUG_REQ(D_ERROR, req,
811 "packing failed for abort-reply; skipping");
813 target_release_saved_req(req);
817 /* Called from a cleanup function if the device is being cleaned up
818 forcefully. The exports should all have been disconnected already,
819 the only thing left to do is
820 - clear the recovery flags
822 - free queued requests and replies, but don't send replies
823 Because the obd_stopping flag is set, no new requests should be received.
826 void target_cleanup_recovery(struct obd_device *obd)
828 struct list_head *tmp, *n;
829 struct ptlrpc_request *req;
832 LASSERT(obd->obd_stopping);
834 spin_lock_bh(&obd->obd_processing_task_lock);
835 if (!obd->obd_recovering) {
836 spin_unlock_bh(&obd->obd_processing_task_lock);
840 obd->obd_recovering = obd->obd_abort_recovery = 0;
841 target_cancel_recovery_timer(obd);
842 spin_unlock_bh(&obd->obd_processing_task_lock);
844 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
845 req = list_entry(tmp, struct ptlrpc_request, rq_list);
846 list_del(&req->rq_list);
847 target_release_saved_req(req);
850 list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
851 req = list_entry(tmp, struct ptlrpc_request, rq_list);
852 list_del(&req->rq_list);
853 target_release_saved_req(req);
858 void target_abort_recovery(void *data)
860 struct obd_device *obd = data;
862 spin_lock_bh(&obd->obd_processing_task_lock);
863 if (!obd->obd_recovering) {
864 spin_unlock_bh(&obd->obd_processing_task_lock);
868 obd->obd_recovering = obd->obd_abort_recovery = 0;
869 obd->obd_recoverable_clients = 0;
870 target_cancel_recovery_timer(obd);
871 spin_unlock_bh(&obd->obd_processing_task_lock);
873 CERROR("%s: recovery period over; disconnecting unfinished clients.\n",
875 class_disconnect_stale_exports(obd);
876 abort_recovery_queue(obd);
878 target_finish_recovery(obd);
880 ptlrpc_run_recovery_over_upcall(obd);
883 static void target_recovery_expired(unsigned long castmeharder)
885 struct obd_device *obd = (struct obd_device *)castmeharder;
886 CERROR("%s: recovery timed out, aborting\n", obd->obd_name);
887 spin_lock_bh(&obd->obd_processing_task_lock);
888 if (obd->obd_recovering)
889 obd->obd_abort_recovery = 1;
890 wake_up(&obd->obd_next_transno_waitq);
891 spin_unlock_bh(&obd->obd_processing_task_lock);
895 /* obd_processing_task_lock should be held */
896 void target_cancel_recovery_timer(struct obd_device *obd)
898 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
899 del_timer(&obd->obd_recovery_timer);
902 static void reset_recovery_timer(struct obd_device *obd)
904 spin_lock_bh(&obd->obd_processing_task_lock);
905 if (!obd->obd_recovering) {
906 spin_unlock_bh(&obd->obd_processing_task_lock);
909 mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
910 spin_unlock_bh(&obd->obd_processing_task_lock);
911 CDEBUG(D_HA, "%s: timer will expire in %u seconds\n", obd->obd_name,
912 (int)(OBD_RECOVERY_TIMEOUT / HZ));
913 /* Only used for lprocfs_status */
914 obd->obd_recovery_end = CURRENT_SECONDS + OBD_RECOVERY_TIMEOUT/HZ;
918 /* Only start it the first time called */
919 void target_start_recovery_timer(struct obd_device *obd, svc_handler_t handler)
921 spin_lock_bh(&obd->obd_processing_task_lock);
922 if (obd->obd_recovery_handler) {
923 spin_unlock_bh(&obd->obd_processing_task_lock);
926 CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
927 (int)(OBD_RECOVERY_TIMEOUT / HZ));
928 obd->obd_recovery_handler = handler;
929 obd->obd_recovery_timer.function = target_recovery_expired;
930 obd->obd_recovery_timer.data = (unsigned long)obd;
931 spin_unlock_bh(&obd->obd_processing_task_lock);
933 reset_recovery_timer(obd);
936 static int check_for_next_transno(struct obd_device *obd)
938 struct ptlrpc_request *req;
939 int wake_up = 0, connected, completed, queue_len, max;
940 __u64 next_transno, req_transno;
942 spin_lock_bh(&obd->obd_processing_task_lock);
943 req = list_entry(obd->obd_recovery_queue.next,
944 struct ptlrpc_request, rq_list);
945 max = obd->obd_max_recoverable_clients;
946 req_transno = req->rq_reqmsg->transno;
947 connected = obd->obd_connected_clients;
948 completed = max - obd->obd_recoverable_clients;
949 queue_len = obd->obd_requests_queued_for_recovery;
950 next_transno = obd->obd_next_recovery_transno;
952 CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
953 "req_transno: "LPU64", next_transno: "LPU64"\n",
954 max, connected, completed, queue_len, req_transno, next_transno);
955 if (obd->obd_abort_recovery) {
956 CDEBUG(D_HA, "waking for aborted recovery\n");
958 } else if (!obd->obd_recovering) {
959 CDEBUG(D_HA, "waking for completed recovery (?)\n");
961 } else if (req_transno == next_transno) {
962 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
964 } else if (queue_len + completed == max) {
966 "waking for skipped transno (skip: "LPD64
967 ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
968 next_transno, queue_len, completed, max, req_transno);
969 obd->obd_next_recovery_transno = req_transno;
972 spin_unlock_bh(&obd->obd_processing_task_lock);
973 LASSERT(req->rq_reqmsg->transno >= next_transno);
977 static void process_recovery_queue(struct obd_device *obd)
979 struct ptlrpc_request *req;
980 int abort_recovery = 0;
981 struct l_wait_info lwi = { 0 };
985 spin_lock_bh(&obd->obd_processing_task_lock);
986 LASSERT(obd->obd_processing_task == current->pid);
987 req = list_entry(obd->obd_recovery_queue.next,
988 struct ptlrpc_request, rq_list);
990 if (req->rq_reqmsg->transno != obd->obd_next_recovery_transno) {
991 spin_unlock_bh(&obd->obd_processing_task_lock);
992 CDEBUG(D_HA, "Waiting for transno "LPD64" (1st is "
994 obd->obd_next_recovery_transno,
995 req->rq_reqmsg->transno);
996 l_wait_event(obd->obd_next_transno_waitq,
997 check_for_next_transno(obd), &lwi);
998 spin_lock_bh(&obd->obd_processing_task_lock);
999 abort_recovery = obd->obd_abort_recovery;
1000 spin_unlock_bh(&obd->obd_processing_task_lock);
1001 if (abort_recovery) {
1002 target_abort_recovery(obd);
1007 list_del_init(&req->rq_list);
1008 obd->obd_requests_queued_for_recovery--;
1009 spin_unlock_bh(&obd->obd_processing_task_lock);
1011 DEBUG_REQ(D_HA, req, "processing: ");
1012 (void)obd->obd_recovery_handler(req);
1013 obd->obd_replayed_requests++;
1014 reset_recovery_timer(obd);
1015 /* bug 1580: decide how to properly sync() in recovery */
1016 //mds_fsync_super(mds->mds_sb);
1017 class_export_put(req->rq_export);
1018 if (req->rq_reply_state != NULL) {
1019 ptlrpc_rs_decref(req->rq_reply_state);
1020 /* req->rq_reply_state = NULL; */
1022 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
1023 OBD_FREE(req, sizeof *req);
1024 spin_lock_bh(&obd->obd_processing_task_lock);
1025 obd->obd_next_recovery_transno++;
1026 if (list_empty(&obd->obd_recovery_queue)) {
1027 obd->obd_processing_task = 0;
1028 spin_unlock_bh(&obd->obd_processing_task_lock);
1031 spin_unlock_bh(&obd->obd_processing_task_lock);
1036 int target_queue_recovery_request(struct ptlrpc_request *req,
1037 struct obd_device *obd)
1039 struct list_head *tmp;
1041 __u64 transno = req->rq_reqmsg->transno;
1042 struct ptlrpc_request *saved_req;
1043 struct lustre_msg *reqmsg;
1045 /* CAVEAT EMPTOR: The incoming request message has been swabbed
1046 * (i.e. buflens etc are in my own byte order), but type-dependent
1047 * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1050 INIT_LIST_HEAD(&req->rq_list);
1051 DEBUG_REQ(D_HA, req, "not queueing");
1055 /* XXX If I were a real man, these LBUGs would be sane cleanups. */
1056 /* XXX just like the request-dup code in queue_final_reply */
1057 OBD_ALLOC(saved_req, sizeof *saved_req);
1060 OBD_ALLOC(reqmsg, req->rq_reqlen);
1064 spin_lock_bh(&obd->obd_processing_task_lock);
1066 /* If we're processing the queue, we want don't want to queue this
1069 * Also, if this request has a transno less than the one we're waiting
1070 * for, we should process it now. It could (and currently always will)
1071 * be an open request for a descriptor that was opened some time ago.
1073 * Also, a resent, replayed request that has already been
1074 * handled will pass through here and be processed immediately.
1076 if (obd->obd_processing_task == current->pid ||
1077 transno < obd->obd_next_recovery_transno) {
1078 /* Processing the queue right now, don't re-add. */
1079 LASSERT(list_empty(&req->rq_list));
1080 spin_unlock_bh(&obd->obd_processing_task_lock);
1081 OBD_FREE(reqmsg, req->rq_reqlen);
1082 OBD_FREE(saved_req, sizeof *saved_req);
1086 /* A resent, replayed request that is still on the queue; just drop it.
1087 The queued request will handle this. */
1088 if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT|MSG_REPLAY)) ==
1089 (MSG_RESENT | MSG_REPLAY)) {
1090 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1091 spin_unlock_bh(&obd->obd_processing_task_lock);
1092 OBD_FREE(reqmsg, req->rq_reqlen);
1093 OBD_FREE(saved_req, sizeof *saved_req);
1097 memcpy(saved_req, req, sizeof *req);
1098 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1100 req->rq_reqmsg = reqmsg;
1101 class_export_get(req->rq_export);
1102 INIT_LIST_HEAD(&req->rq_list);
1105 list_for_each(tmp, &obd->obd_recovery_queue) {
1106 struct ptlrpc_request *reqiter =
1107 list_entry(tmp, struct ptlrpc_request, rq_list);
1109 if (reqiter->rq_reqmsg->transno > transno) {
1110 list_add_tail(&req->rq_list, &reqiter->rq_list);
1117 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
1120 obd->obd_requests_queued_for_recovery++;
1122 if (obd->obd_processing_task != 0) {
1123 /* Someone else is processing this queue, we'll leave it to
1126 wake_up(&obd->obd_next_transno_waitq);
1127 spin_unlock_bh(&obd->obd_processing_task_lock);
1131 /* Nobody is processing, and we know there's (at least) one to process
1132 * now, so we'll do the honours.
1134 obd->obd_processing_task = current->pid;
1135 spin_unlock_bh(&obd->obd_processing_task_lock);
1137 process_recovery_queue(obd);
1141 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1143 return req->rq_export->exp_obd;
1146 int target_queue_final_reply(struct ptlrpc_request *req, int rc)
1148 struct obd_device *obd = target_req2obd(req);
1149 struct ptlrpc_request *saved_req;
1150 struct lustre_msg *reqmsg;
1151 int recovery_done = 0;
1153 LASSERT ((rc == 0) == (req->rq_reply_state != NULL));
1156 /* Just like ptlrpc_error, but without the sending. */
1157 rc = lustre_pack_reply(req, 0, NULL, NULL);
1158 LASSERT(rc == 0); /* XXX handle this */
1159 req->rq_type = PTL_RPC_MSG_ERR;
1162 LASSERT (!req->rq_reply_state->rs_difficult);
1163 LASSERT(list_empty(&req->rq_list));
1164 /* XXX a bit like the request-dup code in queue_recovery_request */
1165 OBD_ALLOC(saved_req, sizeof *saved_req);
1168 OBD_ALLOC(reqmsg, req->rq_reqlen);
1171 memcpy(saved_req, req, sizeof *saved_req);
1172 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1174 /* Don't race cleanup */
1175 spin_lock_bh(&obd->obd_processing_task_lock);
1176 if (obd->obd_stopping) {
1177 spin_unlock_bh(&obd->obd_processing_task_lock);
1178 OBD_FREE(reqmsg, req->rq_reqlen);
1179 OBD_FREE(saved_req, sizeof *req);
1180 req->rq_status = -ENOTCONN;
1181 /* rv is ignored anyhow */
1184 ptlrpc_rs_addref(req->rq_reply_state); /* +1 ref for saved reply */
1186 req->rq_reqmsg = reqmsg;
1187 class_export_get(req->rq_export);
1188 list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
1190 /* only count the first "replay over" request from each
1192 if (req->rq_export->exp_replay_needed) {
1193 --obd->obd_recoverable_clients;
1194 req->rq_export->exp_replay_needed = 0;
1196 recovery_done = (obd->obd_recoverable_clients == 0);
1197 spin_unlock_bh(&obd->obd_processing_task_lock);
1199 OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
1200 if (recovery_done) {
1201 spin_lock_bh(&obd->obd_processing_task_lock);
1202 obd->obd_recovering = obd->obd_abort_recovery = 0;
1203 target_cancel_recovery_timer(obd);
1204 spin_unlock_bh(&obd->obd_processing_task_lock);
1206 target_finish_recovery(obd);
1207 ptlrpc_run_recovery_over_upcall(obd);
1209 CWARN("%s: %d recoverable clients remain\n",
1210 obd->obd_name, obd->obd_recoverable_clients);
1211 wake_up(&obd->obd_next_transno_waitq);
1218 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1220 if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1221 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1222 DEBUG_REQ(D_ERROR, req, "dropping reply");
1227 DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
1228 req->rq_status = rc;
1229 return (ptlrpc_error(req));
1231 DEBUG_REQ(D_NET, req, "sending reply");
1234 return (ptlrpc_send_reply(req, 1));
1238 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1241 unsigned long flags;
1242 struct ptlrpc_reply_state *rs;
1243 struct obd_device *obd;
1244 struct obd_export *exp;
1245 struct ptlrpc_service *svc;
1247 svc = req->rq_rqbd->rqbd_service;
1248 rs = req->rq_reply_state;
1249 if (rs == NULL || !rs->rs_difficult) {
1251 target_send_reply_msg (req, rc, fail_id);
1255 /* must be an export if locks saved */
1256 LASSERT (req->rq_export != NULL);
1257 /* req/reply consistent */
1258 LASSERT (rs->rs_service == svc);
1261 LASSERT (!rs->rs_scheduled);
1262 LASSERT (!rs->rs_scheduled_ever);
1263 LASSERT (!rs->rs_handled);
1264 LASSERT (!rs->rs_on_net);
1265 LASSERT (rs->rs_export == NULL);
1266 LASSERT (list_empty(&rs->rs_obd_list));
1267 LASSERT (list_empty(&rs->rs_exp_list));
1269 exp = class_export_get (req->rq_export);
1272 /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1273 rs->rs_scheduled = 1;
1275 rs->rs_xid = req->rq_xid;
1276 rs->rs_transno = req->rq_transno;
1277 rs->rs_export = exp;
1279 spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
1281 if (rs->rs_transno > obd->obd_last_committed) {
1282 /* not committed already */
1283 list_add_tail (&rs->rs_obd_list,
1284 &obd->obd_uncommitted_replies);
1287 spin_unlock (&obd->obd_uncommitted_replies_lock);
1288 spin_lock (&exp->exp_lock);
1290 list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1292 spin_unlock_irqrestore (&exp->exp_lock, flags);
1294 netrc = target_send_reply_msg (req, rc, fail_id);
1296 spin_lock_irqsave (&svc->srv_lock, flags);
1298 svc->srv_n_difficult_replies++;
1301 /* error sending: reply is off the net. Also we need +1
1302 * reply ref until ptlrpc_server_handle_reply() is done
1303 * with the reply state (if the send was successful, there
1304 * would have been +1 ref for the net, which
1305 * reply_out_callback leaves alone) */
1307 ptlrpc_rs_addref(rs);
1308 atomic_inc (&svc->srv_outstanding_replies);
1311 if (!rs->rs_on_net || /* some notifier */
1312 list_empty(&rs->rs_exp_list) || /* completed already */
1313 list_empty(&rs->rs_obd_list)) {
1314 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1315 wake_up (&svc->srv_waitq);
1317 list_add (&rs->rs_list, &svc->srv_active_replies);
1318 rs->rs_scheduled = 0; /* allow notifier to schedule */
1321 spin_unlock_irqrestore (&svc->srv_lock, flags);
1324 int target_handle_ping(struct ptlrpc_request *req)
1326 return lustre_pack_reply(req, 0, NULL, NULL);
1329 void target_committed_to_req(struct ptlrpc_request *req)
1331 struct obd_device *obd = req->rq_export->exp_obd;
1333 if (!obd->obd_no_transno && req->rq_repmsg != NULL)
1334 req->rq_repmsg->last_committed = obd->obd_last_committed;
1336 DEBUG_REQ(D_IOCTL, req, "not sending last_committed update");
1338 CDEBUG(D_INFO, "last_committed "LPU64", xid "LPU64"\n",
1339 obd->obd_last_committed, req->rq_xid);
1342 EXPORT_SYMBOL(target_committed_to_req);