1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 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 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
45 struct obd_ioctl_data* data = buf;
46 int rq_portal, rp_portal;
48 struct client_obd *cli = &obddev->u.cli;
49 struct obd_import *imp = &cli->cl_import;
50 obd_uuid_t server_uuid;
53 if (obddev->obd_type->typ_ops->o_brw) {
54 rq_portal = OST_REQUEST_PORTAL;
55 rp_portal = OSC_REPLY_PORTAL;
58 rq_portal = MDS_REQUEST_PORTAL;
59 rp_portal = MDC_REPLY_PORTAL;
63 if (data->ioc_inllen1 < 1) {
64 CERROR("requires a TARGET UUID\n");
68 if (data->ioc_inllen1 > 37) {
69 CERROR("client UUID must be less than 38 characters\n");
73 if (data->ioc_inllen2 < 1) {
74 CERROR("setup requires a SERVER UUID\n");
78 if (data->ioc_inllen2 > 37) {
79 CERROR("target UUID must be less than 38 characters\n");
83 sema_init(&cli->cl_sem, 1);
84 cli->cl_conn_count = 0;
85 memcpy(cli->cl_target_uuid, data->ioc_inlbuf1, data->ioc_inllen1);
86 memcpy(server_uuid, data->ioc_inlbuf2, MIN(data->ioc_inllen2,
87 sizeof(server_uuid)));
89 imp->imp_connection = ptlrpc_uuid_to_connection(server_uuid);
90 if (!imp->imp_connection)
93 INIT_LIST_HEAD(&imp->imp_request_list);
94 spin_lock_init(&imp->imp_lock);
96 ptlrpc_init_client(rq_portal, rp_portal, name,
97 &obddev->obd_ldlm_client);
98 imp->imp_client = &obddev->obd_ldlm_client;
99 imp->imp_obd = obddev;
101 cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
107 int client_obd_cleanup(struct obd_device * obddev)
109 struct client_obd *obd = &obddev->u.cli;
111 ptlrpc_cleanup_client(&obd->cl_import);
112 ptlrpc_put_connection(obd->cl_import.imp_connection);
118 int client_obd_connect(struct lustre_handle *conn, struct obd_device *obd,
119 obd_uuid_t cluuid, struct recovd_obd *recovd,
120 ptlrpc_recovery_cb_t recover)
122 struct client_obd *cli = &obd->u.cli;
123 struct ptlrpc_request *request;
124 int rc, size[] = {sizeof(cli->cl_target_uuid),
125 sizeof(obd->obd_uuid) };
126 char *tmp[] = {cli->cl_target_uuid, obd->obd_uuid};
127 int rq_opc = (obd->obd_type->typ_ops->o_brw) ? OST_CONNECT :MDS_CONNECT;
128 struct ptlrpc_connection *c;
129 struct obd_import *imp = &cli->cl_import;
134 rc = class_connect(conn, obd, cluuid);
139 cli->cl_conn_count++;
140 if (cli->cl_conn_count > 1)
143 obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
144 LDLM_NAMESPACE_CLIENT);
145 if (obd->obd_namespace == NULL)
146 GOTO(out_disco, rc = -ENOMEM);
148 INIT_LIST_HEAD(&imp->imp_chain);
149 imp->imp_last_xid = 0;
150 imp->imp_max_transno = 0;
151 imp->imp_peer_last_xid = 0;
152 imp->imp_peer_committed_transno = 0;
154 request = ptlrpc_prep_req(&cli->cl_import, rq_opc, 2, size, tmp);
156 GOTO(out_ldlm, rc = -ENOMEM);
158 request->rq_level = LUSTRE_CONN_NEW;
159 request->rq_replen = lustre_msg_size(0, NULL);
160 request->rq_reqmsg->addr = conn->addr;
161 request->rq_reqmsg->cookie = conn->cookie;
162 c = class_conn2export(conn)->exp_connection =
163 ptlrpc_connection_addref(request->rq_connection);
164 recovd_conn_manage(c, recovd, recover);
166 rc = ptlrpc_queue_wait(request);
170 if (rq_opc == MDS_CONNECT)
171 imp->imp_flags |= IMP_REPLAYABLE;
172 list_add(&imp->imp_chain, &c->c_imports);
173 c->c_level = LUSTRE_CONN_FULL;
174 imp->imp_handle.addr = request->rq_repmsg->addr;
175 imp->imp_handle.cookie = request->rq_repmsg->cookie;
179 ptlrpc_req_finished(request);
182 ldlm_namespace_free(obd->obd_namespace);
183 obd->obd_namespace = NULL;
184 if (rq_opc == MDS_CONNECT) {
185 /* Don't class_disconnect OSCs, because the LOV
186 * cares about them even if they can't connect to the
189 * This is leak-bait, but without either a way to
190 * operate on the osc without an export or separate
191 * methods for connect-to-osc and connect-osc-to-ost
192 * it's not clear what else to do.
195 cli->cl_conn_count--;
196 class_disconnect(conn);
205 int client_obd_disconnect(struct lustre_handle *conn)
207 struct obd_device *obd = class_conn2obd(conn);
208 struct client_obd *cli = &obd->u.cli;
210 struct ptlrpc_request *request = NULL;
215 CERROR("invalid connection for disconnect: addr "LPX64
216 ", cookie "LPX64"\n", conn ? conn->addr : -1UL,
217 conn ? conn->cookie : -1UL);
221 rq_opc = obd->obd_type->typ_ops->o_brw ? OST_DISCONNECT:MDS_DISCONNECT;
223 if (!cli->cl_conn_count) {
224 CERROR("disconnecting disconnected device (%s)\n",
226 GOTO(out_sem, rc = -EINVAL);
229 cli->cl_conn_count--;
230 if (cli->cl_conn_count)
231 GOTO(out_disco, rc = 0);
233 ldlm_namespace_free(obd->obd_namespace);
234 obd->obd_namespace = NULL;
235 request = ptlrpc_prep_req(&cli->cl_import, rq_opc, 0, NULL,
238 GOTO(out_disco, rc = -ENOMEM);
240 request->rq_replen = lustre_msg_size(0, NULL);
242 /* Process disconnects even if we're waiting for recovery. */
243 request->rq_level = LUSTRE_CONN_RECOVD;
245 rc = ptlrpc_queue_wait(request);
252 ptlrpc_req_finished(request);
254 err = class_disconnect(conn);
257 list_del_init(&cli->cl_import.imp_chain);