Whamcloud - gitweb
- Grammatical, LDLM updates to network.lyx
[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 extern struct recovd_obd *ptlrpc_connmgr;
49
50 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
51 {
52         struct obd_ioctl_data* data = buf;
53         int rq_portal = (obddev->obd_type->typ_ops->o_getattr) ? OST_REQUEST_PORTAL : MDS_REQUEST_PORTAL;
54         int rp_portal = (obddev->obd_type->typ_ops->o_getattr) ? OSC_REPLY_PORTAL : MDC_REPLY_PORTAL;
55         struct client_obd *mdc = &obddev->u.cli;
56         char server_uuid[37];
57         int rc;
58         ENTRY;
59
60         if (data->ioc_inllen1 < 1) {
61                 CERROR("requires a TARGET UUID\n");
62                 RETURN(-EINVAL);
63         }
64
65         if (data->ioc_inllen1 > 37) {
66                 CERROR("client UUID must be less than 38 characters\n");
67                 RETURN(-EINVAL);
68         }
69
70         if (data->ioc_inllen2 < 1) {
71                 CERROR("setup requires a SERVER UUID\n");
72                RETURN(-EINVAL);
73         }
74
75         if (data->ioc_inllen2 > 37) {
76                 CERROR("target UUID must be less than 38 characters\n");
77                 RETURN(-EINVAL);
78         }
79
80         sema_init(&mdc->cl_sem, 1);
81         mdc->cl_conn_count = 0;
82         memcpy(mdc->cl_target_uuid, data->ioc_inlbuf1, data->ioc_inllen1);
83         memcpy(server_uuid, data->ioc_inlbuf2, MIN(data->ioc_inllen2,
84                                                    sizeof(server_uuid)));
85
86         mdc->cl_conn = ptlrpc_uuid_to_connection(server_uuid);
87         if (!mdc->cl_conn)
88                 RETURN(-ENOENT);
89
90         OBD_ALLOC(mdc->cl_client, sizeof(*mdc->cl_client));
91         if (mdc->cl_client == NULL)
92                 GOTO(out_conn, rc = -ENOMEM);
93
94         OBD_ALLOC(mdc->cl_ldlm_client, sizeof(*mdc->cl_ldlm_client));
95         if (mdc->cl_ldlm_client == NULL)
96                 GOTO(out_client, rc = -ENOMEM);
97
98         /* XXX get recovery hooked in here again */
99         //ptlrpc_init_client(ptlrpc_connmgr, ll_recover,...
100
101         ptlrpc_init_client(NULL, NULL, rq_portal, rp_portal, mdc->cl_client);
102         ptlrpc_init_client(NULL, NULL, LDLM_REQUEST_PORTAL, LDLM_REPLY_PORTAL,
103                            mdc->cl_ldlm_client);
104         mdc->cl_client->cli_name = "mdc";
105         mdc->cl_ldlm_client->cli_name = "ldlm";
106         mdc->cl_max_mdsize = sizeof(struct lov_stripe_md);
107
108         MOD_INC_USE_COUNT;
109         RETURN(0);
110
111  out_client:
112         OBD_FREE(mdc->cl_client, sizeof(*mdc->cl_client));
113  out_conn:
114         ptlrpc_put_connection(mdc->cl_conn);
115         return rc;
116 }
117
118 int client_obd_cleanup(struct obd_device * obddev)
119 {
120         struct client_obd *mdc = &obddev->u.cli;
121
122         ptlrpc_cleanup_client(mdc->cl_client);
123         OBD_FREE(mdc->cl_client, sizeof(*mdc->cl_client));
124         ptlrpc_cleanup_client(mdc->cl_ldlm_client);
125         OBD_FREE(mdc->cl_ldlm_client, sizeof(*mdc->cl_ldlm_client));
126         ptlrpc_put_connection(mdc->cl_conn);
127
128         MOD_DEC_USE_COUNT;
129         return 0;
130 }
131
132 int client_obd_connect(struct lustre_handle *conn, struct obd_device *obd)
133 {
134         struct client_obd *cli = &obd->u.cli;
135         struct ptlrpc_request *request;
136         int rc, size[] = {sizeof(cli->cl_target_uuid),
137                           sizeof(obd->obd_uuid) };
138         char *tmp[] = {cli->cl_target_uuid, obd->obd_uuid};
139         int rq_opc = (obd->obd_type->typ_ops->o_getattr) ? OST_CONNECT : MDS_CONNECT;
140
141         ENTRY;
142         down(&cli->cl_sem);
143         MOD_INC_USE_COUNT;
144         rc = class_connect(conn, obd);
145         if (!rc)
146                 cli->cl_conn_count++;
147         else {
148                 MOD_DEC_USE_COUNT;
149                 up(&cli->cl_sem);
150                 RETURN(rc);
151         }
152
153         if (cli->cl_conn_count > 1) {
154                 up(&cli->cl_sem);
155                 RETURN(0);
156         }
157
158         obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
159                                                 LDLM_NAMESPACE_CLIENT);
160         if (obd->obd_namespace == NULL) {
161                 up(&cli->cl_sem);
162                 RETURN(-ENOMEM);
163         }
164
165         request = ptlrpc_prep_req(cli->cl_client, cli->cl_conn, rq_opc, 2, size,
166                                   tmp);
167         if (!request)
168                 GOTO(out_disco, -ENOMEM);
169
170         request->rq_level = LUSTRE_CONN_NEW;
171         request->rq_replen = lustre_msg_size(0, NULL);
172         //   This handle may be important if a callback needs
173         //   to find the mdc/osc
174         //        request->rq_reqmsg->addr = conn->addr;
175         //        request->rq_reqmsg->cookie = conn->cookie;
176
177         rc = ptlrpc_queue_wait(request);
178         rc = ptlrpc_check_status(request, rc);
179         if (rc)
180                 GOTO(out, rc);
181
182         request->rq_connection->c_level = LUSTRE_CONN_FULL;
183         cli->cl_exporth = *(struct lustre_handle *)request->rq_repmsg;
184
185         EXIT;
186  out:
187         ptlrpc_free_req(request);
188  out_disco:
189         if (rc) {
190                 class_disconnect(conn);
191                 MOD_DEC_USE_COUNT;
192         }
193         up(&cli->cl_sem);
194         return rc;
195 }
196
197 int client_obd_disconnect(struct lustre_handle *conn)
198 {
199         struct obd_device *obd = class_conn2obd(conn);
200         int rq_opc = (obd->obd_type->typ_ops->o_getattr) ? OST_DISCONNECT : MDS_DISCONNECT;
201         struct ptlrpc_request *request = NULL;
202         int rc;
203         ENTRY;
204
205         down(&obd->u.cli.cl_sem);
206         if (!obd->u.cli.cl_conn_count) {
207                 CERROR("disconnecting disconnected device (%s)\n",
208                        obd->obd_name);
209                 RETURN(0);
210         }
211
212         obd->u.cli.cl_conn_count--;
213         if (obd->u.cli.cl_conn_count)
214                 GOTO(class_only, 0);
215
216         ldlm_namespace_free(obd->obd_namespace);
217         obd->obd_namespace = NULL;
218         request = ptlrpc_prep_req2(conn, rq_opc, 0, NULL, NULL);
219         if (!request)
220                 RETURN(-ENOMEM);
221
222         request->rq_replen = lustre_msg_size(0, NULL);
223
224         rc = ptlrpc_queue_wait(request);
225         if (rc)
226                 GOTO(out, rc);
227  class_only:
228         rc = class_disconnect(conn);
229         if (!rc)
230                 MOD_DEC_USE_COUNT;
231  out:
232         if (request)
233                 ptlrpc_free_req(request);
234         up(&obd->u.cli.cl_sem);
235         RETURN(rc);
236 }
237
238 int target_handle_connect(struct ptlrpc_request *req)
239 {
240         struct obd_device *target;
241         struct obd_export *export;
242         struct lustre_handle conn;
243         char *tgtuuid, *cluuid;
244         int rc, i;
245         ENTRY;
246
247         tgtuuid = lustre_msg_buf(req->rq_reqmsg, 0);
248         if (req->rq_reqmsg->buflens[0] > 37) {
249                 /* Invalid UUID */
250                 req->rq_status = -EINVAL;
251                 RETURN(-EINVAL);
252         }
253
254         cluuid =  lustre_msg_buf(req->rq_reqmsg, 1);
255         if (req->rq_reqmsg->buflens[1] > 37) {
256                 /* Invalid UUID */
257                 req->rq_status = -EINVAL;
258                 RETURN(-EINVAL);
259         }
260
261         i = class_uuid2dev(tgtuuid);
262         if (i == -1) {
263                 req->rq_status = -ENODEV;
264                 RETURN(-ENODEV);
265         }
266
267         target = &obd_dev[i];
268         if (!target) {
269                 req->rq_status = -ENODEV;
270                 RETURN(-ENODEV);
271         }
272
273         conn.addr = req->rq_reqmsg->addr;
274         conn.cookie = req->rq_reqmsg->cookie;
275
276         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
277         if (rc)
278                 RETURN(rc);
279
280         req->rq_status = obd_connect(&conn, target);
281         req->rq_repmsg->addr = conn.addr;
282         req->rq_repmsg->cookie = conn.cookie;
283
284         export = class_conn2export(&conn);
285         if (!export)
286                 LBUG();
287
288         req->rq_export = export;
289         export->exp_connection = req->rq_connection;
290 #warning Peter: is this the right place to upgrade the server connection level?
291         req->rq_connection->c_level = LUSTRE_CONN_FULL;
292         RETURN(0);
293 }
294
295 int target_handle_disconnect(struct ptlrpc_request *req)
296 {
297         struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
298         int rc;
299         ENTRY;
300
301         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
302         if (rc)
303                 RETURN(rc);
304
305         req->rq_status = obd_disconnect(conn);
306         RETURN(0);
307 }