1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001-2003 Cluster File Systems, Inc.
5 * Author: Peter J. Braam <braam@clusterfs.com>
6 * Author: Phil Schwan <phil@clusterfs.com>
7 * Author: Mike Shaver <shaver@clusterfs.com>
9 * This file is part of Lustre, http://www.lustre.org.
11 * Lustre is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General Public
13 * License as published by the Free Software Foundation.
15 * Lustre is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Lustre; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * Client-common OBD method implementations and utility functions.
28 #define DEBUG_SUBSYSTEM S_OST /* XXX WRONG */
30 #include <linux/module.h>
31 #include <linux/obd_ost.h>
32 #include <linux/lustre_net.h>
33 #include <linux/lustre_dlm.h>
35 struct client_obd *client_conn2cli(struct lustre_handle *conn)
37 struct obd_export *export = class_conn2export(conn);
40 return &export->exp_obd->u.cli;
43 struct obd_device *client_tgtuuid2obd(struct obd_uuid *tgtuuid)
47 for (i = 0; i < MAX_OBD_DEVICES; i++) {
48 struct obd_device *obd = &obd_dev[i];
49 if ((strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0) ||
50 (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0)) {
51 struct client_obd *cli = &obd->u.cli;
52 if (strncmp(tgtuuid->uuid, cli->cl_target_uuid.uuid,
53 sizeof(cli->cl_target_uuid.uuid)) == 0)
61 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
63 struct obd_ioctl_data* data = buf;
64 int rq_portal, rp_portal;
66 struct client_obd *cli = &obddev->u.cli;
67 struct obd_import *imp = &cli->cl_import;
68 struct obd_uuid server_uuid;
71 if (obddev->obd_type->typ_ops->o_brw) {
72 rq_portal = OST_REQUEST_PORTAL;
73 rp_portal = OSC_REPLY_PORTAL;
76 rq_portal = MDS_REQUEST_PORTAL;
77 rp_portal = MDC_REPLY_PORTAL;
81 if (data->ioc_inllen1 < 1) {
82 CERROR("requires a TARGET UUID\n");
86 if (data->ioc_inllen1 > 37) {
87 CERROR("client UUID must be less than 38 characters\n");
91 if (data->ioc_inllen2 < 1) {
92 CERROR("setup requires a SERVER UUID\n");
96 if (data->ioc_inllen2 > 37) {
97 CERROR("target UUID must be less than 38 characters\n");
101 sema_init(&cli->cl_sem, 1);
102 cli->cl_conn_count = 0;
103 memcpy(cli->cl_target_uuid.uuid, data->ioc_inlbuf1, data->ioc_inllen1);
104 memcpy(server_uuid.uuid, data->ioc_inlbuf2, MIN(data->ioc_inllen2,
105 sizeof(server_uuid)));
107 imp->imp_connection = ptlrpc_uuid_to_connection(&server_uuid);
108 if (!imp->imp_connection)
111 INIT_LIST_HEAD(&imp->imp_replay_list);
112 INIT_LIST_HEAD(&imp->imp_sending_list);
113 INIT_LIST_HEAD(&imp->imp_delayed_list);
114 spin_lock_init(&imp->imp_lock);
116 ptlrpc_init_client(rq_portal, rp_portal, name,
117 &obddev->obd_ldlm_client);
118 imp->imp_client = &obddev->obd_ldlm_client;
119 imp->imp_obd = obddev;
121 cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
126 int client_obd_cleanup(struct obd_device * obddev)
128 struct client_obd *obd = &obddev->u.cli;
130 ptlrpc_cleanup_client(&obd->cl_import);
131 ptlrpc_put_connection(obd->cl_import.imp_connection);
136 int client_obd_connect(struct lustre_handle *conn, struct obd_device *obd,
137 struct obd_uuid *cluuid, struct recovd_obd *recovd,
138 ptlrpc_recovery_cb_t recover)
140 struct client_obd *cli = &obd->u.cli;
141 struct ptlrpc_request *request;
142 int rc, size[] = {sizeof(cli->cl_target_uuid),
143 sizeof(obd->obd_uuid) };
144 char *tmp[] = {cli->cl_target_uuid.uuid, obd->obd_uuid.uuid};
145 int rq_opc = (obd->obd_type->typ_ops->o_brw) ? OST_CONNECT :MDS_CONNECT;
146 struct ptlrpc_connection *c;
147 struct obd_import *imp = &cli->cl_import;
152 rc = class_connect(conn, obd, cluuid);
156 cli->cl_conn_count++;
157 if (cli->cl_conn_count > 1)
160 if (obd->obd_namespace != NULL)
161 CERROR("already have namespace!\n");
162 obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
163 LDLM_NAMESPACE_CLIENT);
164 if (obd->obd_namespace == NULL)
165 GOTO(out_disco, rc = -ENOMEM);
167 INIT_LIST_HEAD(&imp->imp_chain);
168 imp->imp_last_xid = 0;
169 imp->imp_max_transno = 0;
170 imp->imp_peer_committed_transno = 0;
172 request = ptlrpc_prep_req(&cli->cl_import, rq_opc, 2, size, tmp);
174 GOTO(out_ldlm, rc = -ENOMEM);
176 request->rq_level = LUSTRE_CONN_NEW;
177 request->rq_replen = lustre_msg_size(0, NULL);
178 request->rq_reqmsg->addr = conn->addr;
179 request->rq_reqmsg->cookie = conn->cookie;
180 c = class_conn2export(conn)->exp_connection =
181 ptlrpc_connection_addref(request->rq_connection);
182 list_add(&imp->imp_chain, &c->c_imports);
183 recovd_conn_manage(c, recovd, recover);
185 imp->imp_level = LUSTRE_CONN_CON;
186 rc = ptlrpc_queue_wait(request);
190 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
191 if (rq_opc == MDS_CONNECT || msg_flags & MSG_CONNECT_REPLAYABLE) {
192 imp->imp_flags |= IMP_REPLAYABLE;
193 CDEBUG(D_HA, "connected to replayable target: %s\n", cli->cl_target_uuid.uuid);
195 imp->imp_level = LUSTRE_CONN_FULL;
196 imp->imp_handle.addr = request->rq_repmsg->addr;
197 imp->imp_handle.cookie = request->rq_repmsg->cookie;
201 ptlrpc_req_finished(request);
204 ldlm_namespace_free(obd->obd_namespace);
205 obd->obd_namespace = NULL;
206 if (rq_opc == MDS_CONNECT) {
207 /* Don't class_disconnect OSCs, because the LOV
208 * cares about them even if they can't connect to the
211 * This is leak-bait, but without either a way to
212 * operate on the osc without an export or separate
213 * methods for connect-to-osc and connect-osc-to-ost
214 * it's not clear what else to do.
217 cli->cl_conn_count--;
218 class_disconnect(conn);
226 int client_obd_disconnect(struct lustre_handle *conn)
228 struct obd_device *obd = class_conn2obd(conn);
229 struct client_obd *cli = &obd->u.cli;
231 struct ptlrpc_request *request = NULL;
236 CERROR("invalid connection for disconnect: addr "LPX64
237 ", cookie "LPX64"\n", conn ? conn->addr : -1UL,
238 conn ? conn->cookie : -1UL);
242 rq_opc = obd->obd_type->typ_ops->o_brw ? OST_DISCONNECT:MDS_DISCONNECT;
244 if (!cli->cl_conn_count) {
245 CERROR("disconnecting disconnected device (%s)\n",
247 GOTO(out_sem, rc = -EINVAL);
250 cli->cl_conn_count--;
251 if (cli->cl_conn_count)
252 GOTO(out_no_disconnect, rc = 0);
254 if (obd->obd_namespace != NULL) {
255 ldlm_cli_cancel_unused(obd->obd_namespace, NULL, 0);
256 ldlm_namespace_free(obd->obd_namespace);
257 obd->obd_namespace = NULL;
259 request = ptlrpc_prep_req(&cli->cl_import, rq_opc, 0, NULL, NULL);
261 GOTO(out_req, rc = -ENOMEM);
263 request->rq_replen = lustre_msg_size(0, NULL);
265 /* Process disconnects even if we're waiting for recovery. */
266 request->rq_level = LUSTRE_CONN_RECOVD;
268 rc = ptlrpc_queue_wait(request);
275 ptlrpc_req_finished(request);
276 list_del_init(&cli->cl_import.imp_chain);
278 err = class_disconnect(conn);