Whamcloud - gitweb
Install and distribute lconf and lmc.
[fs/lustre-release.git] / lustre / lib / l_net.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
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  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  *  Storage Target Handling functions
24  *  Lustre Object Server Module (OST)
25  *
26  *  This server is single threaded at present (but can easily be multi
27  *  threaded). For testing and management it is treated as an
28  *  obd_device, although it does not export a full OBD method table
29  *  (the requests are coming in over the wire, so object target
30  *  modules do not have a full method table.)
31  */
32
33 #define EXPORT_SYMTAB
34 #define DEBUG_SUBSYSTEM S_OST
35
36 #include <linux/module.h>
37 #include <linux/obd_ost.h>
38 #include <linux/lustre_net.h>
39 #include <linux/lustre_dlm.h>
40
41 struct client_obd *client_conn2cli(struct lustre_handle *conn)
42 {
43         struct obd_export *export = class_conn2export(conn);
44         if (!export)
45                 LBUG();
46         return &export->exp_obd->u.cli;
47 }
48
49 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
50 {
51         struct obd_ioctl_data* data = buf;
52         int rq_portal = (obddev->obd_type->typ_ops->o_brw) ? OST_REQUEST_PORTAL : MDS_REQUEST_PORTAL;
53         int rp_portal = (obddev->obd_type->typ_ops->o_brw) ? OSC_REPLY_PORTAL : MDC_REPLY_PORTAL;
54         struct client_obd *mdc = &obddev->u.cli;
55         char server_uuid[37];
56         int rc;
57         ENTRY;
58
59         if (data->ioc_inllen1 < 1) {
60                 CERROR("requires a TARGET UUID\n");
61                 RETURN(-EINVAL);
62         }
63
64         if (data->ioc_inllen1 > 37) {
65                 CERROR("client UUID must be less than 38 characters\n");
66                 RETURN(-EINVAL);
67         }
68
69         if (data->ioc_inllen2 < 1) {
70                 CERROR("setup requires a SERVER UUID\n");
71                 RETURN(-EINVAL);
72         }
73
74         if (data->ioc_inllen2 > 37) {
75                 CERROR("target UUID must be less than 38 characters\n");
76                 RETURN(-EINVAL);
77         }
78
79         sema_init(&mdc->cl_sem, 1);
80         mdc->cl_conn_count = 0;
81         memcpy(mdc->cl_target_uuid, data->ioc_inlbuf1, data->ioc_inllen1);
82         memcpy(server_uuid, data->ioc_inlbuf2, MIN(data->ioc_inllen2,
83                                                    sizeof(server_uuid)));
84
85         mdc->cl_conn = ptlrpc_uuid_to_connection(server_uuid);
86         if (!mdc->cl_conn)
87                 RETURN(-ENOENT);
88
89         OBD_ALLOC(mdc->cl_client, sizeof(*mdc->cl_client));
90         if (mdc->cl_client == NULL)
91                 GOTO(out_conn, rc = -ENOMEM);
92
93         OBD_ALLOC(mdc->cl_ldlm_client, sizeof(*mdc->cl_ldlm_client));
94         if (mdc->cl_ldlm_client == NULL)
95                 GOTO(out_client, rc = -ENOMEM);
96
97         ptlrpc_init_client(rq_portal, rp_portal, mdc->cl_client, mdc->cl_conn);
98         ptlrpc_init_client(LDLM_REQUEST_PORTAL, LDLM_REPLY_PORTAL,
99                            mdc->cl_ldlm_client, mdc->cl_conn);
100         mdc->cl_client->cli_name = "mdc";
101         mdc->cl_ldlm_client->cli_name = "ldlm";
102         mdc->cl_max_mdsize = sizeof(struct lov_mds_md);
103
104         MOD_INC_USE_COUNT;
105         RETURN(0);
106
107  out_client:
108         OBD_FREE(mdc->cl_client, sizeof(*mdc->cl_client));
109  out_conn:
110         ptlrpc_put_connection(mdc->cl_conn);
111         return rc;
112 }
113
114 int client_obd_cleanup(struct obd_device * obddev)
115 {
116         struct client_obd *mdc = &obddev->u.cli;
117
118         ptlrpc_cleanup_client(mdc->cl_client);
119         OBD_FREE(mdc->cl_client, sizeof(*mdc->cl_client));
120         ptlrpc_cleanup_client(mdc->cl_ldlm_client);
121         OBD_FREE(mdc->cl_ldlm_client, sizeof(*mdc->cl_ldlm_client));
122         ptlrpc_put_connection(mdc->cl_conn);
123
124         MOD_DEC_USE_COUNT;
125         return 0;
126 }
127
128 int client_obd_connect(struct lustre_handle *conn, struct obd_device *obd,
129                        char *cluuid)
130 {
131         struct client_obd *cli = &obd->u.cli;
132         struct ptlrpc_request *request;
133         int rc, size[] = {sizeof(cli->cl_target_uuid),
134                           sizeof(obd->obd_uuid) };
135         char *tmp[] = {cli->cl_target_uuid, obd->obd_uuid};
136         int rq_opc = (obd->obd_type->typ_ops->o_brw) ? OST_CONNECT :MDS_CONNECT;
137
138         ENTRY;
139         down(&cli->cl_sem);
140         MOD_INC_USE_COUNT;
141         rc = class_connect(conn, obd, cluuid);
142         if (rc) {
143                 MOD_DEC_USE_COUNT;
144                 GOTO(out_sem, rc);
145         }
146
147         cli->cl_conn_count++;
148         if (cli->cl_conn_count > 1)
149                 GOTO(out_sem, rc);
150
151         obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
152                                                 LDLM_NAMESPACE_CLIENT);
153         if (obd->obd_namespace == NULL)
154                 GOTO(out_disco, rc = -ENOMEM);
155
156         request = ptlrpc_prep_req(cli->cl_client, rq_opc, 2, size, tmp);
157         if (!request)
158                 GOTO(out_ldlm, rc = -ENOMEM);
159
160         request->rq_level = LUSTRE_CONN_NEW;
161         request->rq_replen = lustre_msg_size(0, NULL);
162         //   This handle may be important if a callback needs
163         //   to find the mdc/osc
164         //        request->rq_reqmsg->addr = conn->addr;
165         //        request->rq_reqmsg->cookie = conn->cookie;
166
167         rc = ptlrpc_queue_wait(request);
168         rc = ptlrpc_check_status(request, rc);
169         if (rc)
170                 GOTO(out_req, rc);
171
172         request->rq_connection->c_level = LUSTRE_CONN_FULL;
173         cli->cl_exporth = *(struct lustre_handle *)request->rq_repmsg;
174
175         EXIT;
176 out_req:
177         ptlrpc_free_req(request);
178         if (rc) {
179 out_ldlm:
180                 ldlm_namespace_free(obd->obd_namespace);
181                 obd->obd_namespace = NULL;
182 out_disco:
183                 class_disconnect(conn);
184                 MOD_DEC_USE_COUNT;
185         }
186 out_sem:
187         up(&cli->cl_sem);
188         return rc;
189 }
190
191 int client_obd_disconnect(struct lustre_handle *conn)
192 {
193         struct obd_device *obd = class_conn2obd(conn);
194         struct client_obd *cli = &obd->u.cli;
195         int rq_opc = (obd->obd_type->typ_ops->o_brw) ? OST_DISCONNECT : MDS_DISCONNECT;
196         struct ptlrpc_request *request = NULL;
197         int rc, err;
198         ENTRY;
199
200         down(&cli->cl_sem);
201         if (!cli->cl_conn_count) {
202                 CERROR("disconnecting disconnected device (%s)\n",
203                        obd->obd_name);
204                 GOTO(out_sem, rc = -EINVAL);
205         }
206
207         cli->cl_conn_count--;
208         if (cli->cl_conn_count)
209                 GOTO(out_disco, rc = 0);
210
211         ldlm_namespace_free(obd->obd_namespace);
212         obd->obd_namespace = NULL;
213         request = ptlrpc_prep_req2(conn, rq_opc, 0, NULL, NULL);
214         if (!request)
215                 GOTO(out_disco, rc = -ENOMEM);
216
217         request->rq_replen = lustre_msg_size(0, NULL);
218
219         rc = ptlrpc_queue_wait(request);
220         if (rc)
221                 GOTO(out_req, rc);
222
223         EXIT;
224  out_req:
225         if (request)
226                 ptlrpc_free_req(request);
227  out_disco:
228         err = class_disconnect(conn);
229         if (!rc && err)
230                 rc = err;
231         MOD_DEC_USE_COUNT;
232  out_sem:
233         up(&cli->cl_sem);
234         RETURN(rc);
235 }
236
237 int target_handle_connect(struct ptlrpc_request *req)
238 {
239         struct obd_device *target;
240         struct obd_export *export;
241         struct lustre_handle conn;
242         char *tgtuuid, *cluuid;
243         int rc, i;
244         ENTRY;
245
246         tgtuuid = lustre_msg_buf(req->rq_reqmsg, 0);
247         if (req->rq_reqmsg->buflens[0] > 37) {
248                 CERROR("bad target UUID for connect\n");
249                 GOTO(out, rc = -EINVAL);
250         }
251
252         cluuid = lustre_msg_buf(req->rq_reqmsg, 1);
253         if (req->rq_reqmsg->buflens[1] > 37) {
254                 CERROR("bad client UUID for connect\n");
255                 GOTO(out, rc = -EINVAL);
256         }
257
258         i = class_uuid2dev(tgtuuid);
259         if (i == -1) {
260                 CERROR("UUID '%s' not found for connect\n", tgtuuid);
261                 GOTO(out, rc = -ENODEV);
262         }
263
264         target = &obd_dev[i];
265         if (!target)
266                 GOTO(out, rc = -ENODEV);
267
268         conn.addr = req->rq_reqmsg->addr;
269         conn.cookie = req->rq_reqmsg->cookie;
270
271         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
272         if (rc)
273                 GOTO(out, rc);
274
275         rc = obd_connect(&conn, target, cluuid);
276         if (rc)
277                 GOTO(out, rc);
278         req->rq_repmsg->addr = conn.addr;
279         req->rq_repmsg->cookie = conn.cookie;
280
281         export = class_conn2export(&conn);
282         LASSERT(export);
283
284         req->rq_export = export;
285         export->exp_connection = req->rq_connection;
286         ptlrpc_init_client(LDLM_REQUEST_PORTAL, LDLM_REPLY_PORTAL,
287                            &export->exp_ldlm_data.led_client,
288                            export->exp_connection);
289
290 #warning Peter: is this the right place to upgrade the server connection level?
291         req->rq_connection->c_level = LUSTRE_CONN_FULL;
292 out:
293         req->rq_status = rc;
294         RETURN(rc);
295 }
296
297 int target_handle_disconnect(struct ptlrpc_request *req)
298 {
299         struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
300         int rc;
301         ENTRY;
302
303         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
304         if (rc)
305                 RETURN(rc);
306
307         req->rq_status = obd_disconnect(conn);
308         RETURN(0);
309 }