Whamcloud - gitweb
9a7ae9614ae1de75ef9b1fe92f02cc4b480ad0d6
[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, rp_portal;
53         char *name;
54         struct client_obd *cli = &obddev->u.cli;
55         char server_uuid[37];
56         ENTRY;
57
58         if (obddev->obd_type->typ_ops->o_brw) {
59                 rq_portal = OST_REQUEST_PORTAL;
60                 rp_portal = OSC_REPLY_PORTAL;
61                 name = "osc";
62         } else {
63                 rq_portal = MDS_REQUEST_PORTAL;
64                 rp_portal = MDC_REPLY_PORTAL;
65                 name = "mdc";
66         }
67
68         if (data->ioc_inllen1 < 1) {
69                 CERROR("requires a TARGET UUID\n");
70                 RETURN(-EINVAL);
71         }
72
73         if (data->ioc_inllen1 > 37) {
74                 CERROR("client UUID must be less than 38 characters\n");
75                 RETURN(-EINVAL);
76         }
77
78         if (data->ioc_inllen2 < 1) {
79                 CERROR("setup requires a SERVER UUID\n");
80                 RETURN(-EINVAL);
81         }
82
83         if (data->ioc_inllen2 > 37) {
84                 CERROR("target UUID must be less than 38 characters\n");
85                 RETURN(-EINVAL);
86         }
87
88         sema_init(&cli->cl_sem, 1);
89         cli->cl_conn_count = 0;
90         memcpy(cli->cl_target_uuid, data->ioc_inlbuf1, data->ioc_inllen1);
91         memcpy(server_uuid, data->ioc_inlbuf2, MIN(data->ioc_inllen2,
92                                                    sizeof(server_uuid)));
93
94         cli->cl_import.imp_connection = ptlrpc_uuid_to_connection(server_uuid);
95         if (!cli->cl_import.imp_connection)
96                 RETURN(-ENOENT);
97
98         ptlrpc_init_client(rq_portal, rp_portal, name,
99                            &obddev->obd_ldlm_client);
100         cli->cl_import.imp_client = &obddev->obd_ldlm_client;
101         cli->cl_import.imp_obd = obddev;
102
103         cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
104         cli->cl_max_ost_easize = sizeof(struct lov_stripe_md);
105
106         MOD_INC_USE_COUNT;
107         RETURN(0);
108 }
109
110 int client_obd_cleanup(struct obd_device * obddev)
111 {
112         struct client_obd *mdc = &obddev->u.cli;
113
114         ptlrpc_cleanup_client(&mdc->cl_import);
115         ptlrpc_put_connection(mdc->cl_import.imp_connection);
116
117         MOD_DEC_USE_COUNT;
118         return 0;
119 }
120
121 int client_obd_connect(struct lustre_handle *conn, struct obd_device *obd,
122                        char *cluuid)
123 {
124         struct client_obd *cli = &obd->u.cli;
125         struct ptlrpc_request *request;
126         int rc, size[] = {sizeof(cli->cl_target_uuid),
127                           sizeof(obd->obd_uuid) };
128         char *tmp[] = {cli->cl_target_uuid, obd->obd_uuid};
129         int rq_opc = (obd->obd_type->typ_ops->o_brw) ? OST_CONNECT :MDS_CONNECT;
130         struct ptlrpc_connection *c;
131
132         ENTRY;
133         down(&cli->cl_sem);
134         MOD_INC_USE_COUNT;
135         rc = class_connect(conn, obd, cluuid);
136         if (rc) {
137                 MOD_DEC_USE_COUNT;
138                 GOTO(out_sem, rc);
139         }
140         cli->cl_conn_count++;
141         if (cli->cl_conn_count > 1)
142                 GOTO(out_sem, rc);
143
144         obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
145                                                 LDLM_NAMESPACE_CLIENT);
146         if (obd->obd_namespace == NULL)
147                 GOTO(out_disco, rc = -ENOMEM);
148
149         request = ptlrpc_prep_req(&cli->cl_import, rq_opc, 2, size, tmp);
150         if (!request)
151                 GOTO(out_ldlm, rc = -ENOMEM);
152
153         request->rq_level = LUSTRE_CONN_NEW;
154         request->rq_replen = lustre_msg_size(0, NULL);
155         request->rq_reqmsg->addr = conn->addr;
156         request->rq_reqmsg->cookie = conn->cookie;
157         c = class_conn2export(conn)->exp_connection = request->rq_connection;
158
159         rc = ptlrpc_queue_wait(request);
160         rc = ptlrpc_check_status(request, rc);
161         if (rc)
162                 GOTO(out_req, rc);
163
164         list_add(&cli->cl_import.imp_chain, &c->c_imports);
165         c->c_level = LUSTRE_CONN_FULL;
166         cli->cl_import.imp_handle.addr = request->rq_repmsg->addr;
167         cli->cl_import.imp_handle.cookie = request->rq_repmsg->cookie;
168
169         recovd_conn_manage(c, ptlrpc_recovd, ll_recover);
170
171         EXIT;
172 out_req:
173         ptlrpc_free_req(request);
174         if (rc) {
175 out_ldlm:
176                 ldlm_namespace_free(obd->obd_namespace);
177                 obd->obd_namespace = NULL;
178 out_disco:
179                 class_disconnect(conn);
180                 MOD_DEC_USE_COUNT;
181         }
182 out_sem:
183         up(&cli->cl_sem);
184         return rc;
185 }
186
187 int client_obd_disconnect(struct lustre_handle *conn)
188 {
189         struct obd_device *obd = class_conn2obd(conn);
190         struct client_obd *cli = &obd->u.cli;
191         int rq_opc = (obd->obd_type->typ_ops->o_brw) ? OST_DISCONNECT : MDS_DISCONNECT;
192         struct ptlrpc_request *request = NULL;
193         int rc, err;
194         ENTRY;
195
196         down(&cli->cl_sem);
197         if (!cli->cl_conn_count) {
198                 CERROR("disconnecting disconnected device (%s)\n",
199                        obd->obd_name);
200                 GOTO(out_sem, rc = -EINVAL);
201         }
202
203         cli->cl_conn_count--;
204         if (cli->cl_conn_count)
205                 GOTO(out_disco, rc = 0);
206
207         ldlm_namespace_free(obd->obd_namespace);
208         obd->obd_namespace = NULL;
209         request = ptlrpc_prep_req(&cli->cl_import, rq_opc, 0, NULL, NULL);
210         if (!request)
211                 GOTO(out_disco, rc = -ENOMEM);
212
213         request->rq_replen = lustre_msg_size(0, NULL);
214
215         rc = ptlrpc_queue_wait(request);
216         if (rc)
217                 GOTO(out_req, rc);
218
219         EXIT;
220  out_req:
221         if (request)
222                 ptlrpc_free_req(request);
223  out_disco:
224         err = class_disconnect(conn);
225         if (!rc && err)
226                 rc = err;
227         list_del_init(&cli->cl_import.imp_chain);
228         MOD_DEC_USE_COUNT;
229  out_sem:
230         up(&cli->cl_sem);
231         RETURN(rc);
232 }
233
234 int target_handle_connect(struct ptlrpc_request *req)
235 {
236         struct obd_device *target;
237         struct obd_export *export;
238         struct obd_import *dlmimp;
239         struct lustre_handle conn;
240         char *tgtuuid, *cluuid;
241         int rc, i;
242         ENTRY;
243
244         tgtuuid = lustre_msg_buf(req->rq_reqmsg, 0);
245         if (req->rq_reqmsg->buflens[0] > 37) {
246                 CERROR("bad target UUID for connect\n");
247                 GOTO(out, rc = -EINVAL);
248         }
249
250         cluuid = lustre_msg_buf(req->rq_reqmsg, 1);
251         if (req->rq_reqmsg->buflens[1] > 37) {
252                 CERROR("bad client UUID for connect\n");
253                 GOTO(out, rc = -EINVAL);
254         }
255
256         i = class_uuid2dev(tgtuuid);
257         if (i == -1) {
258                 CERROR("UUID '%s' not found for connect\n", tgtuuid);
259                 GOTO(out, rc = -ENODEV);
260         }
261
262         target = &obd_dev[i];
263         if (!target)
264                 GOTO(out, rc = -ENODEV);
265
266         conn.addr = req->rq_reqmsg->addr;
267         conn.cookie = req->rq_reqmsg->cookie;
268
269         rc = obd_connect(&conn, target, cluuid);
270         if (rc)
271                 GOTO(out, rc);
272
273         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
274         if (rc)
275                 GOTO(out, rc);
276         req->rq_repmsg->addr = conn.addr;
277         req->rq_repmsg->cookie = conn.cookie;
278
279         export = class_conn2export(&conn);
280         LASSERT(export);
281
282         req->rq_export = export;
283         export->exp_connection = ptlrpc_get_connection(&req->rq_peer, cluuid);
284         req->rq_connection = export->exp_connection;
285
286         spin_lock(&export->exp_connection->c_lock);
287         list_add(&export->exp_conn_chain, &export->exp_connection->c_exports);
288         spin_unlock(&export->exp_connection->c_lock);
289
290         recovd_conn_manage(export->exp_connection, ptlrpc_recovd,
291                            target_revoke_connection);
292         dlmimp = &export->exp_ldlm_data.led_import;
293         dlmimp->imp_connection = req->rq_connection;
294         dlmimp->imp_client = &export->exp_obd->obd_ldlm_client;
295         dlmimp->imp_handle.addr = req->rq_reqmsg->addr;
296         dlmimp->imp_handle.cookie = req->rq_reqmsg->cookie;
297         dlmimp->imp_obd = /* LDLM! */ NULL;
298         
299 #warning Peter: is this the right place to upgrade the server connection level?
300         req->rq_connection->c_level = LUSTRE_CONN_FULL;
301 out:
302         req->rq_status = rc;
303         RETURN(rc);
304 }
305
306 int target_handle_disconnect(struct ptlrpc_request *req)
307 {
308         struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
309         int rc;
310         ENTRY;
311
312         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
313         if (rc)
314                 RETURN(rc);
315
316         req->rq_status = obd_disconnect(conn);
317         RETURN(0);
318 }
319
320 static int target_revoke_client_resources(struct ptlrpc_connection *conn)
321 {
322         struct list_head *tmp, *pos;
323
324         ENTRY;
325
326         /* Cancel outstanding locks. */
327         list_for_each_safe(tmp, pos, &conn->c_exports) {
328         }
329
330         RETURN(0);
331 }
332
333 static int target_fence_failed_connection(struct ptlrpc_connection *conn)
334 {
335         ENTRY;
336
337         conn->c_level = LUSTRE_CONN_RECOVD;
338
339         RETURN(0);
340 }
341
342 int target_revoke_connection(struct recovd_data *rd, int phase)
343 {
344         struct ptlrpc_connection *conn = class_rd2conn(rd);
345         
346         LASSERT(conn);
347         ENTRY;
348
349         switch (phase) {
350             case PTLRPC_RECOVD_PHASE_PREPARE:
351                 RETURN(target_fence_failed_connection(conn));
352             case PTLRPC_RECOVD_PHASE_RECOVER:
353                 RETURN(target_revoke_client_resources(conn));
354             case PTLRPC_RECOVD_PHASE_FAILURE:
355                 LBUG();
356                 RETURN(0);
357         }
358
359         LBUG();
360         RETURN(-ENOSYS);
361 }