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