Whamcloud - gitweb
Merge of b_md to 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, 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 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
44 {
45         struct obd_ioctl_data* data = buf;
46         int rq_portal, rp_portal;
47         char *name;
48         struct client_obd *cli = &obddev->u.cli;
49         struct obd_import *imp = &cli->cl_import;
50         obd_uuid_t server_uuid;
51         ENTRY;
52
53         if (obddev->obd_type->typ_ops->o_brw) {
54                 rq_portal = OST_REQUEST_PORTAL;
55                 rp_portal = OSC_REPLY_PORTAL;
56                 name = "osc";
57         } else {
58                 rq_portal = MDS_REQUEST_PORTAL;
59                 rp_portal = MDC_REPLY_PORTAL;
60                 name = "mdc";
61         }
62
63         if (data->ioc_inllen1 < 1) {
64                 CERROR("requires a TARGET UUID\n");
65                 RETURN(-EINVAL);
66         }
67
68         if (data->ioc_inllen1 > 37) {
69                 CERROR("client UUID must be less than 38 characters\n");
70                 RETURN(-EINVAL);
71         }
72
73         if (data->ioc_inllen2 < 1) {
74                 CERROR("setup requires a SERVER UUID\n");
75                 RETURN(-EINVAL);
76         }
77
78         if (data->ioc_inllen2 > 37) {
79                 CERROR("target UUID must be less than 38 characters\n");
80                 RETURN(-EINVAL);
81         }
82
83         sema_init(&cli->cl_sem, 1);
84         cli->cl_conn_count = 0;
85         memcpy(cli->cl_target_uuid, data->ioc_inlbuf1, data->ioc_inllen1);
86         memcpy(server_uuid, data->ioc_inlbuf2, MIN(data->ioc_inllen2,
87                                                    sizeof(server_uuid)));
88
89         imp->imp_connection = ptlrpc_uuid_to_connection(server_uuid);
90         if (!imp->imp_connection)
91                 RETURN(-ENOENT);
92         
93         INIT_LIST_HEAD(&imp->imp_replay_list);
94         INIT_LIST_HEAD(&imp->imp_sending_list);
95         INIT_LIST_HEAD(&imp->imp_delayed_list);
96         spin_lock_init(&imp->imp_lock);
97
98         ptlrpc_init_client(rq_portal, rp_portal, name,
99                            &obddev->obd_ldlm_client);
100         imp->imp_client = &obddev->obd_ldlm_client;
101         imp->imp_obd = obddev;
102
103         cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
104
105         MOD_INC_USE_COUNT;
106         RETURN(0);
107 }
108
109 int client_obd_cleanup(struct obd_device * obddev)
110 {
111         struct client_obd *obd = &obddev->u.cli;
112
113         ptlrpc_cleanup_client(&obd->cl_import);
114         ptlrpc_put_connection(obd->cl_import.imp_connection);
115
116         MOD_DEC_USE_COUNT;
117         return 0;
118 }
119
120 int client_obd_connect(struct lustre_handle *conn, struct obd_device *obd,
121                        obd_uuid_t cluuid, struct recovd_obd *recovd,
122                        ptlrpc_recovery_cb_t recover)
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         struct obd_import *imp = &cli->cl_import;
132
133         ENTRY;
134         down(&cli->cl_sem);
135         MOD_INC_USE_COUNT;
136         rc = class_connect(conn, obd, cluuid);
137         if (rc) {
138                 MOD_DEC_USE_COUNT;
139                 GOTO(out_sem, rc);
140         }
141         cli->cl_conn_count++;
142         if (cli->cl_conn_count > 1)
143                 GOTO(out_sem, rc);
144
145         if (obd->obd_namespace != NULL)
146                 CERROR("already have namespace!\n");
147         obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
148                                                 LDLM_NAMESPACE_CLIENT);
149         if (obd->obd_namespace == NULL)
150                 GOTO(out_disco, rc = -ENOMEM);
151
152         INIT_LIST_HEAD(&imp->imp_chain);
153         imp->imp_last_xid = 0;
154         imp->imp_max_transno = 0;
155         imp->imp_peer_last_xid = 0;
156         imp->imp_peer_committed_transno = 0;
157
158         request = ptlrpc_prep_req(&cli->cl_import, rq_opc, 2, size, tmp);
159         if (!request)
160                 GOTO(out_ldlm, rc = -ENOMEM);
161
162         request->rq_level = LUSTRE_CONN_NEW;
163         request->rq_replen = lustre_msg_size(0, NULL);
164         request->rq_reqmsg->addr = conn->addr;
165         request->rq_reqmsg->cookie = conn->cookie;
166         c = class_conn2export(conn)->exp_connection =
167                 ptlrpc_connection_addref(request->rq_connection);
168         list_add(&imp->imp_chain, &c->c_imports);
169         recovd_conn_manage(c, recovd, recover);
170
171         imp->imp_level = LUSTRE_CONN_CON;
172         rc = ptlrpc_queue_wait(request);
173         if (rc)
174                 GOTO(out_req, rc);
175
176         if (rq_opc == MDS_CONNECT)
177                 imp->imp_flags |= IMP_REPLAYABLE;
178         imp->imp_level = LUSTRE_CONN_FULL;
179         imp->imp_handle.addr = request->rq_repmsg->addr;
180         imp->imp_handle.cookie = request->rq_repmsg->cookie;
181
182         EXIT;
183 out_req:
184         ptlrpc_req_finished(request);
185         if (rc) {
186 out_ldlm:
187                 ldlm_namespace_free(obd->obd_namespace);
188                 obd->obd_namespace = NULL;
189                 if (rq_opc == MDS_CONNECT) {
190                         /* Don't class_disconnect OSCs, because the LOV
191                          * cares about them even if they can't connect to the
192                          * OST.
193                          *
194                          * This is leak-bait, but without either a way to
195                          * operate on the osc without an export or separate
196                          * methods for connect-to-osc and connect-osc-to-ost
197                          * it's not clear what else to do.
198                          */
199 out_disco:
200                         cli->cl_conn_count--;
201                         class_disconnect(conn);
202                         MOD_DEC_USE_COUNT;
203                 }
204         }
205 out_sem:
206         up(&cli->cl_sem);
207         return rc;
208 }
209
210 int client_obd_disconnect(struct lustre_handle *conn)
211 {
212         struct obd_device *obd = class_conn2obd(conn);
213         struct client_obd *cli = &obd->u.cli;
214         int rq_opc;
215         struct ptlrpc_request *request = NULL;
216         int rc, err;
217         ENTRY;
218
219         if (!obd) {
220                 CERROR("invalid connection for disconnect: addr "LPX64
221                        ", cookie "LPX64"\n", conn ? conn->addr : -1UL,
222                        conn ? conn->cookie : -1UL);
223                 RETURN(-EINVAL);
224         }
225
226         rq_opc = obd->obd_type->typ_ops->o_brw ? OST_DISCONNECT:MDS_DISCONNECT;
227         down(&cli->cl_sem);
228         if (!cli->cl_conn_count) {
229                 CERROR("disconnecting disconnected device (%s)\n",
230                        obd->obd_name);
231                 GOTO(out_sem, rc = -EINVAL);
232         }
233
234         cli->cl_conn_count--;
235         if (cli->cl_conn_count)
236                 GOTO(out_disco, rc = 0);
237
238         ldlm_namespace_free(obd->obd_namespace);
239         obd->obd_namespace = NULL;
240         request = ptlrpc_prep_req(&cli->cl_import, rq_opc, 0, NULL,
241                                   NULL);
242         if (!request)
243                 GOTO(out_disco, rc = -ENOMEM);
244         
245         request->rq_replen = lustre_msg_size(0, NULL);
246
247         /* Process disconnects even if we're waiting for recovery. */
248         request->rq_level = LUSTRE_CONN_RECOVD;
249         
250         rc = ptlrpc_queue_wait(request);
251         if (rc)
252                 GOTO(out_req, rc);
253
254         EXIT;
255  out_req:
256         if (request)
257                 ptlrpc_req_finished(request);
258  out_disco:
259         err = class_disconnect(conn);
260         if (!rc && err)
261                 rc = err;
262         list_del_init(&cli->cl_import.imp_chain);
263         MOD_DEC_USE_COUNT;
264  out_sem:
265         up(&cli->cl_sem);
266         RETURN(rc);
267 }