Whamcloud - gitweb
Landing of b_recovery (at last).
[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_request_list);
94         spin_lock_init(&imp->imp_lock);
95
96         ptlrpc_init_client(rq_portal, rp_portal, name,
97                            &obddev->obd_ldlm_client);
98         imp->imp_client = &obddev->obd_ldlm_client;
99         imp->imp_obd = obddev;
100
101         cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
102
103         MOD_INC_USE_COUNT;
104         RETURN(0);
105 }
106
107 int client_obd_cleanup(struct obd_device * obddev)
108 {
109         struct client_obd *obd = &obddev->u.cli;
110
111         ptlrpc_cleanup_client(&obd->cl_import);
112         ptlrpc_put_connection(obd->cl_import.imp_connection);
113
114         MOD_DEC_USE_COUNT;
115         return 0;
116 }
117
118 int client_obd_connect(struct lustre_handle *conn, struct obd_device *obd,
119                        obd_uuid_t cluuid, struct recovd_obd *recovd,
120                        ptlrpc_recovery_cb_t recover)
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         struct ptlrpc_connection *c;
129         struct obd_import *imp = &cli->cl_import;
130
131         ENTRY;
132         down(&cli->cl_sem);
133         MOD_INC_USE_COUNT;
134         rc = class_connect(conn, obd, cluuid);
135         if (rc) {
136                 MOD_DEC_USE_COUNT;
137                 GOTO(out_sem, rc);
138         }
139         cli->cl_conn_count++;
140         if (cli->cl_conn_count > 1)
141                 GOTO(out_sem, rc);
142
143         obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
144                                                 LDLM_NAMESPACE_CLIENT);
145         if (obd->obd_namespace == NULL)
146                 GOTO(out_disco, rc = -ENOMEM);
147
148         INIT_LIST_HEAD(&imp->imp_chain);
149         imp->imp_last_xid = 0;
150         imp->imp_max_transno = 0;
151         imp->imp_peer_last_xid = 0;
152         imp->imp_peer_committed_transno = 0;
153
154         request = ptlrpc_prep_req(&cli->cl_import, rq_opc, 2, size, tmp);
155         if (!request)
156                 GOTO(out_ldlm, rc = -ENOMEM);
157
158         request->rq_level = LUSTRE_CONN_NEW;
159         request->rq_replen = lustre_msg_size(0, NULL);
160         request->rq_reqmsg->addr = conn->addr;
161         request->rq_reqmsg->cookie = conn->cookie;
162         c = class_conn2export(conn)->exp_connection =
163                 ptlrpc_connection_addref(request->rq_connection);
164         recovd_conn_manage(c, recovd, recover);
165
166         rc = ptlrpc_queue_wait(request);
167         if (rc)
168                 GOTO(out_req, rc);
169
170         if (rq_opc == MDS_CONNECT)
171                 imp->imp_flags |= IMP_REPLAYABLE;
172         list_add(&imp->imp_chain, &c->c_imports);
173         c->c_level = LUSTRE_CONN_FULL;
174         imp->imp_handle.addr = request->rq_repmsg->addr;
175         imp->imp_handle.cookie = request->rq_repmsg->cookie;
176
177         EXIT;
178 out_req:
179         ptlrpc_req_finished(request);
180         if (rc) {
181 out_ldlm:
182                 ldlm_namespace_free(obd->obd_namespace);
183                 obd->obd_namespace = NULL;
184                 if (rq_opc == MDS_CONNECT) {
185                         /* Don't class_disconnect OSCs, because the LOV
186                          * cares about them even if they can't connect to the
187                          * OST.
188                          *
189                          * This is leak-bait, but without either a way to
190                          * operate on the osc without an export or separate
191                          * methods for connect-to-osc and connect-osc-to-ost
192                          * it's not clear what else to do.
193                          */
194 out_disco:
195                         cli->cl_conn_count--;
196                         class_disconnect(conn);
197                         MOD_DEC_USE_COUNT;
198                 }
199         }
200 out_sem:
201         up(&cli->cl_sem);
202         return rc;
203 }
204
205 int client_obd_disconnect(struct lustre_handle *conn)
206 {
207         struct obd_device *obd = class_conn2obd(conn);
208         struct client_obd *cli = &obd->u.cli;
209         int rq_opc;
210         struct ptlrpc_request *request = NULL;
211         int rc, err;
212         ENTRY;
213
214         if (!obd) {
215                 CERROR("invalid connection for disconnect: addr "LPX64
216                        ", cookie "LPX64"\n", conn ? conn->addr : -1UL,
217                        conn ? conn->cookie : -1UL);
218                 RETURN(-EINVAL);
219         }
220
221         rq_opc = obd->obd_type->typ_ops->o_brw ? OST_DISCONNECT:MDS_DISCONNECT;
222         down(&cli->cl_sem);
223         if (!cli->cl_conn_count) {
224                 CERROR("disconnecting disconnected device (%s)\n",
225                        obd->obd_name);
226                 GOTO(out_sem, rc = -EINVAL);
227         }
228
229         cli->cl_conn_count--;
230         if (cli->cl_conn_count)
231                 GOTO(out_disco, rc = 0);
232
233         ldlm_namespace_free(obd->obd_namespace);
234         obd->obd_namespace = NULL;
235         request = ptlrpc_prep_req(&cli->cl_import, rq_opc, 0, NULL,
236                                   NULL);
237         if (!request)
238                 GOTO(out_disco, rc = -ENOMEM);
239         
240         request->rq_replen = lustre_msg_size(0, NULL);
241
242         /* Process disconnects even if we're waiting for recovery. */
243         request->rq_level = LUSTRE_CONN_RECOVD;
244         
245         rc = ptlrpc_queue_wait(request);
246         if (rc)
247                 GOTO(out_req, rc);
248
249         EXIT;
250  out_req:
251         if (request)
252                 ptlrpc_req_finished(request);
253  out_disco:
254         err = class_disconnect(conn);
255         if (!rc && err)
256                 rc = err;
257         list_del_init(&cli->cl_import.imp_chain);
258         MOD_DEC_USE_COUNT;
259  out_sem:
260         up(&cli->cl_sem);
261         RETURN(rc);
262 }