Whamcloud - gitweb
Merge b_md into HEAD
[fs/lustre-release.git] / lustre / lib / client.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001-2003 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>
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
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.
14  *
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.
19  *
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.
23  *
24  * Client-common OBD method implementations and utility functions.
25  */
26
27 #define EXPORT_SYMTAB
28 #define DEBUG_SUBSYSTEM S_OST /* XXX WRONG */
29
30 #include <linux/module.h>
31 #include <linux/obd_ost.h>
32 #include <linux/lustre_net.h>
33 #include <linux/lustre_dlm.h>
34
35 struct client_obd *client_conn2cli(struct lustre_handle *conn)
36 {
37         struct obd_export *export = class_conn2export(conn);
38         if (!export)
39                 LBUG();
40         return &export->exp_obd->u.cli;
41 }
42
43 struct obd_device *client_tgtuuid2obd(struct obd_uuid *tgtuuid)
44 {
45         int i;
46
47         for (i = 0; i < MAX_OBD_DEVICES; i++) {
48                 struct obd_device *obd = &obd_dev[i];
49                 if ((strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0) ||
50                     (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0)) {
51                         struct client_obd *cli = &obd->u.cli;
52                         if (strncmp(tgtuuid->uuid, cli->cl_target_uuid.uuid,
53                                     sizeof(cli->cl_target_uuid.uuid)) == 0)
54                                 return obd;
55                 }
56         }
57
58         return NULL;
59 }
60
61 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
62 {
63         struct obd_ioctl_data* data = buf;
64         int rq_portal, rp_portal;
65         char *name;
66         struct client_obd *cli = &obddev->u.cli;
67         struct obd_import *imp = &cli->cl_import;
68         struct obd_uuid server_uuid;
69         ENTRY;
70
71         if (obddev->obd_type->typ_ops->o_brw) {
72                 rq_portal = OST_REQUEST_PORTAL;
73                 rp_portal = OSC_REPLY_PORTAL;
74                 name = "osc";
75         } else {
76                 rq_portal = MDS_REQUEST_PORTAL;
77                 rp_portal = MDC_REPLY_PORTAL;
78                 name = "mdc";
79         }
80
81         if (data->ioc_inllen1 < 1) {
82                 CERROR("requires a TARGET UUID\n");
83                 RETURN(-EINVAL);
84         }
85
86         if (data->ioc_inllen1 > 37) {
87                 CERROR("client UUID must be less than 38 characters\n");
88                 RETURN(-EINVAL);
89         }
90
91         if (data->ioc_inllen2 < 1) {
92                 CERROR("setup requires a SERVER UUID\n");
93                 RETURN(-EINVAL);
94         }
95
96         if (data->ioc_inllen2 > 37) {
97                 CERROR("target UUID must be less than 38 characters\n");
98                 RETURN(-EINVAL);
99         }
100
101         sema_init(&cli->cl_sem, 1);
102         cli->cl_conn_count = 0;
103         memcpy(cli->cl_target_uuid.uuid, data->ioc_inlbuf1, data->ioc_inllen1);
104         memcpy(server_uuid.uuid, data->ioc_inlbuf2, MIN(data->ioc_inllen2,
105                                                    sizeof(server_uuid)));
106
107         imp->imp_connection = ptlrpc_uuid_to_connection(&server_uuid);
108         if (!imp->imp_connection)
109                 RETURN(-ENOENT);
110
111         INIT_LIST_HEAD(&imp->imp_replay_list);
112         INIT_LIST_HEAD(&imp->imp_sending_list);
113         INIT_LIST_HEAD(&imp->imp_delayed_list);
114         spin_lock_init(&imp->imp_lock);
115
116         ptlrpc_init_client(rq_portal, rp_portal, name,
117                            &obddev->obd_ldlm_client);
118         imp->imp_client = &obddev->obd_ldlm_client;
119         imp->imp_obd = obddev;
120
121         cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
122
123         RETURN(0);
124 }
125
126 int client_obd_cleanup(struct obd_device * obddev)
127 {
128         struct client_obd *obd = &obddev->u.cli;
129
130         ptlrpc_cleanup_client(&obd->cl_import);
131         ptlrpc_put_connection(obd->cl_import.imp_connection);
132
133         return 0;
134 }
135
136 int client_obd_connect(struct lustre_handle *conn, struct obd_device *obd,
137                        struct obd_uuid *cluuid, struct recovd_obd *recovd,
138                        ptlrpc_recovery_cb_t recover)
139 {
140         struct client_obd *cli = &obd->u.cli;
141         struct ptlrpc_request *request;
142         int rc, size[] = {sizeof(cli->cl_target_uuid),
143                           sizeof(obd->obd_uuid) };
144         char *tmp[] = {cli->cl_target_uuid.uuid, obd->obd_uuid.uuid};
145         int rq_opc = (obd->obd_type->typ_ops->o_brw) ? OST_CONNECT :MDS_CONNECT;
146         struct ptlrpc_connection *c;
147         struct obd_import *imp = &cli->cl_import;
148         int msg_flags;
149
150         ENTRY;
151         down(&cli->cl_sem);
152         rc = class_connect(conn, obd, cluuid);
153         if (rc)
154                 GOTO(out_sem, rc);
155
156         cli->cl_conn_count++;
157         if (cli->cl_conn_count > 1)
158                 GOTO(out_sem, rc);
159
160         if (obd->obd_namespace != NULL)
161                 CERROR("already have namespace!\n");
162         obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
163                                                 LDLM_NAMESPACE_CLIENT);
164         if (obd->obd_namespace == NULL)
165                 GOTO(out_disco, rc = -ENOMEM);
166
167         INIT_LIST_HEAD(&imp->imp_chain);
168         imp->imp_last_xid = 0;
169         imp->imp_max_transno = 0;
170         imp->imp_peer_committed_transno = 0;
171
172         request = ptlrpc_prep_req(&cli->cl_import, rq_opc, 2, size, tmp);
173         if (!request)
174                 GOTO(out_ldlm, rc = -ENOMEM);
175
176         request->rq_level = LUSTRE_CONN_NEW;
177         request->rq_replen = lustre_msg_size(0, NULL);
178         request->rq_reqmsg->addr = conn->addr;
179         request->rq_reqmsg->cookie = conn->cookie;
180         c = class_conn2export(conn)->exp_connection =
181                 ptlrpc_connection_addref(request->rq_connection);
182         list_add(&imp->imp_chain, &c->c_imports);
183         recovd_conn_manage(c, recovd, recover);
184
185         imp->imp_level = LUSTRE_CONN_CON;
186         rc = ptlrpc_queue_wait(request);
187         if (rc)
188                 GOTO(out_req, rc);
189
190         msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
191         if (rq_opc == MDS_CONNECT || msg_flags & MSG_CONNECT_REPLAYABLE) {
192                 imp->imp_flags |= IMP_REPLAYABLE;
193                 CDEBUG(D_HA, "connected to replayable target: %s\n", cli->cl_target_uuid.uuid);
194         }
195         imp->imp_level = LUSTRE_CONN_FULL;
196         imp->imp_handle.addr = request->rq_repmsg->addr;
197         imp->imp_handle.cookie = request->rq_repmsg->cookie;
198
199         EXIT;
200 out_req:
201         ptlrpc_req_finished(request);
202         if (rc) {
203 out_ldlm:
204                 ldlm_namespace_free(obd->obd_namespace);
205                 obd->obd_namespace = NULL;
206                 if (rq_opc == MDS_CONNECT) {
207                         /* Don't class_disconnect OSCs, because the LOV
208                          * cares about them even if they can't connect to the
209                          * OST.
210                          *
211                          * This is leak-bait, but without either a way to
212                          * operate on the osc without an export or separate
213                          * methods for connect-to-osc and connect-osc-to-ost
214                          * it's not clear what else to do.
215                          */
216 out_disco:
217                         cli->cl_conn_count--;
218                         class_disconnect(conn);
219                 }
220         }
221 out_sem:
222         up(&cli->cl_sem);
223         return rc;
224 }
225
226 int client_obd_disconnect(struct lustre_handle *conn)
227 {
228         struct obd_device *obd = class_conn2obd(conn);
229         struct client_obd *cli = &obd->u.cli;
230         int rq_opc;
231         struct ptlrpc_request *request = NULL;
232         int rc, err;
233         ENTRY;
234
235         if (!obd) {
236                 CERROR("invalid connection for disconnect: addr "LPX64
237                        ", cookie "LPX64"\n", conn ? conn->addr : -1UL,
238                        conn ? conn->cookie : -1UL);
239                 RETURN(-EINVAL);
240         }
241
242         rq_opc = obd->obd_type->typ_ops->o_brw ? OST_DISCONNECT:MDS_DISCONNECT;
243         down(&cli->cl_sem);
244         if (!cli->cl_conn_count) {
245                 CERROR("disconnecting disconnected device (%s)\n",
246                        obd->obd_name);
247                 GOTO(out_sem, rc = -EINVAL);
248         }
249
250         cli->cl_conn_count--;
251         if (cli->cl_conn_count)
252                 GOTO(out_no_disconnect, rc = 0);
253
254         if (obd->obd_namespace != NULL) {
255                 ldlm_cli_cancel_unused(obd->obd_namespace, NULL, 0);
256                 ldlm_namespace_free(obd->obd_namespace);
257                 obd->obd_namespace = NULL;
258         }
259         request = ptlrpc_prep_req(&cli->cl_import, rq_opc, 0, NULL, NULL);
260         if (!request)
261                 GOTO(out_req, rc = -ENOMEM);
262
263         request->rq_replen = lustre_msg_size(0, NULL);
264
265         /* Process disconnects even if we're waiting for recovery. */
266         request->rq_level = LUSTRE_CONN_RECOVD;
267
268         rc = ptlrpc_queue_wait(request);
269         if (rc)
270                 GOTO(out_req, rc);
271
272         EXIT;
273  out_req:
274         if (request)
275                 ptlrpc_req_finished(request);
276         list_del_init(&cli->cl_import.imp_chain);
277  out_no_disconnect:
278         err = class_disconnect(conn);
279         if (!rc && err)
280                 rc = err;
281  out_sem:
282         up(&cli->cl_sem);
283         RETURN(rc);
284 }