Whamcloud - gitweb
03fa4e2a59ca2d9bf50d2d7c222f7d30d26861cb
[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, 2002 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(char *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, cli->cl_target_uuid, 
53                                     sizeof(cli->cl_target_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         obd_uuid_t 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, data->ioc_inlbuf1, data->ioc_inllen1);
104         memcpy(server_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         MOD_INC_USE_COUNT;
124         RETURN(0);
125 }
126
127 int client_obd_cleanup(struct obd_device * obddev)
128 {
129         struct client_obd *obd = &obddev->u.cli;
130
131         ptlrpc_cleanup_client(&obd->cl_import);
132         ptlrpc_put_connection(obd->cl_import.imp_connection);
133
134         MOD_DEC_USE_COUNT;
135         return 0;
136 }
137
138 int client_obd_connect(struct lustre_handle *conn, struct obd_device *obd,
139                        obd_uuid_t cluuid, struct recovd_obd *recovd,
140                        ptlrpc_recovery_cb_t recover)
141 {
142         struct client_obd *cli = &obd->u.cli;
143         struct ptlrpc_request *request;
144         int rc, size[] = {sizeof(cli->cl_target_uuid),
145                           sizeof(obd->obd_uuid) };
146         char *tmp[] = {cli->cl_target_uuid, obd->obd_uuid};
147         int rq_opc = (obd->obd_type->typ_ops->o_brw) ? OST_CONNECT :MDS_CONNECT;
148         struct ptlrpc_connection *c;
149         struct obd_import *imp = &cli->cl_import;
150
151         ENTRY;
152         down(&cli->cl_sem);
153         MOD_INC_USE_COUNT;
154         rc = class_connect(conn, obd, cluuid);
155         if (rc) {
156                 MOD_DEC_USE_COUNT;
157                 GOTO(out_sem, rc);
158         }
159         cli->cl_conn_count++;
160         if (cli->cl_conn_count > 1)
161                 GOTO(out_sem, rc);
162
163         if (obd->obd_namespace != NULL)
164                 CERROR("already have namespace!\n");
165         obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
166                                                 LDLM_NAMESPACE_CLIENT);
167         if (obd->obd_namespace == NULL)
168                 GOTO(out_disco, rc = -ENOMEM);
169
170         INIT_LIST_HEAD(&imp->imp_chain);
171         imp->imp_last_xid = 0;
172         imp->imp_max_transno = 0;
173         imp->imp_peer_last_xid = 0;
174         imp->imp_peer_committed_transno = 0;
175
176         request = ptlrpc_prep_req(&cli->cl_import, rq_opc, 2, size, tmp);
177         if (!request)
178                 GOTO(out_ldlm, rc = -ENOMEM);
179
180         request->rq_level = LUSTRE_CONN_NEW;
181         request->rq_replen = lustre_msg_size(0, NULL);
182         request->rq_reqmsg->addr = conn->addr;
183         request->rq_reqmsg->cookie = conn->cookie;
184         c = class_conn2export(conn)->exp_connection =
185                 ptlrpc_connection_addref(request->rq_connection);
186         list_add(&imp->imp_chain, &c->c_imports);
187         recovd_conn_manage(c, recovd, recover);
188
189         imp->imp_level = LUSTRE_CONN_CON;
190         rc = ptlrpc_queue_wait(request);
191         if (rc)
192                 GOTO(out_req, rc);
193
194         if (rq_opc == MDS_CONNECT)
195                 imp->imp_flags |= IMP_REPLAYABLE;
196         imp->imp_level = LUSTRE_CONN_FULL;
197         imp->imp_handle.addr = request->rq_repmsg->addr;
198         imp->imp_handle.cookie = request->rq_repmsg->cookie;
199
200         EXIT;
201 out_req:
202         ptlrpc_req_finished(request);
203         if (rc) {
204 out_ldlm:
205                 ldlm_namespace_free(obd->obd_namespace);
206                 obd->obd_namespace = NULL;
207                 if (rq_opc == MDS_CONNECT) {
208                         /* Don't class_disconnect OSCs, because the LOV
209                          * cares about them even if they can't connect to the
210                          * OST.
211                          *
212                          * This is leak-bait, but without either a way to
213                          * operate on the osc without an export or separate
214                          * methods for connect-to-osc and connect-osc-to-ost
215                          * it's not clear what else to do.
216                          */
217 out_disco:
218                         cli->cl_conn_count--;
219                         class_disconnect(conn);
220                         MOD_DEC_USE_COUNT;
221                 }
222         }
223 out_sem:
224         up(&cli->cl_sem);
225         return rc;
226 }
227
228 int client_obd_disconnect(struct lustre_handle *conn)
229 {
230         struct obd_device *obd = class_conn2obd(conn);
231         struct client_obd *cli = &obd->u.cli;
232         int rq_opc;
233         struct ptlrpc_request *request = NULL;
234         int rc, err;
235         ENTRY;
236
237         if (!obd) {
238                 CERROR("invalid connection for disconnect: addr "LPX64
239                        ", cookie "LPX64"\n", conn ? conn->addr : -1UL,
240                        conn ? conn->cookie : -1UL);
241                 RETURN(-EINVAL);
242         }
243
244         rq_opc = obd->obd_type->typ_ops->o_brw ? OST_DISCONNECT:MDS_DISCONNECT;
245         down(&cli->cl_sem);
246         if (!cli->cl_conn_count) {
247                 CERROR("disconnecting disconnected device (%s)\n",
248                        obd->obd_name);
249                 GOTO(out_sem, rc = -EINVAL);
250         }
251
252         cli->cl_conn_count--;
253         if (cli->cl_conn_count)
254                 GOTO(out_disco, rc = 0);
255
256         ldlm_namespace_free(obd->obd_namespace);
257         obd->obd_namespace = NULL;
258         request = ptlrpc_prep_req(&cli->cl_import, rq_opc, 0, NULL,
259                                   NULL);
260         if (!request)
261                 GOTO(out_disco, 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  out_disco:
277         err = class_disconnect(conn);
278         if (!rc && err)
279                 rc = err;
280         list_del_init(&cli->cl_import.imp_chain);
281         MOD_DEC_USE_COUNT;
282  out_sem:
283         up(&cli->cl_sem);
284         RETURN(rc);
285 }