Whamcloud - gitweb
Only store replayable (MDC, with transno) requests on the sending_head after
[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         obd_uuid_t server_uuid;
50         ENTRY;
51
52         if (obddev->obd_type->typ_ops->o_brw) {
53                 rq_portal = OST_REQUEST_PORTAL;
54                 rp_portal = OSC_REPLY_PORTAL;
55                 name = "osc";
56         } else {
57                 rq_portal = MDS_REQUEST_PORTAL;
58                 rp_portal = MDC_REPLY_PORTAL;
59                 name = "mdc";
60         }
61
62         if (data->ioc_inllen1 < 1) {
63                 CERROR("requires a TARGET UUID\n");
64                 RETURN(-EINVAL);
65         }
66
67         if (data->ioc_inllen1 > 37) {
68                 CERROR("client UUID must be less than 38 characters\n");
69                 RETURN(-EINVAL);
70         }
71
72         if (data->ioc_inllen2 < 1) {
73                 CERROR("setup requires a SERVER UUID\n");
74                 RETURN(-EINVAL);
75         }
76
77         if (data->ioc_inllen2 > 37) {
78                 CERROR("target UUID must be less than 38 characters\n");
79                 RETURN(-EINVAL);
80         }
81
82         sema_init(&cli->cl_sem, 1);
83         cli->cl_conn_count = 0;
84         memcpy(cli->cl_target_uuid, data->ioc_inlbuf1, data->ioc_inllen1);
85         memcpy(server_uuid, data->ioc_inlbuf2, MIN(data->ioc_inllen2,
86                                                    sizeof(server_uuid)));
87
88         cli->cl_import.imp_connection = ptlrpc_uuid_to_connection(server_uuid);
89         if (!cli->cl_import.imp_connection)
90                 RETURN(-ENOENT);
91
92         ptlrpc_init_client(rq_portal, rp_portal, name,
93                            &obddev->obd_ldlm_client);
94         cli->cl_import.imp_client = &obddev->obd_ldlm_client;
95         cli->cl_import.imp_obd = obddev;
96
97         cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
98         cli->cl_max_ost_easize = sizeof(struct lov_stripe_md);
99
100         MOD_INC_USE_COUNT;
101         RETURN(0);
102 }
103
104 int client_obd_cleanup(struct obd_device * obddev)
105 {
106         struct client_obd *obd = &obddev->u.cli;
107
108         ptlrpc_cleanup_client(&obd->cl_import);
109         ptlrpc_put_connection(obd->cl_import.imp_connection);
110
111         MOD_DEC_USE_COUNT;
112         return 0;
113 }
114
115 int client_obd_connect(struct lustre_handle *conn, struct obd_device *obd,
116                        obd_uuid_t cluuid, struct recovd_obd *recovd,
117                        ptlrpc_recovery_cb_t recover)
118 {
119         struct client_obd *cli = &obd->u.cli;
120         struct ptlrpc_request *request;
121         int rc, size[] = {sizeof(cli->cl_target_uuid),
122                           sizeof(obd->obd_uuid) };
123         char *tmp[] = {cli->cl_target_uuid, obd->obd_uuid};
124         int rq_opc = (obd->obd_type->typ_ops->o_brw) ? OST_CONNECT :MDS_CONNECT;
125         struct ptlrpc_connection *c;
126
127         ENTRY;
128         down(&cli->cl_sem);
129         MOD_INC_USE_COUNT;
130         rc = class_connect(conn, obd, cluuid);
131         if (rc) {
132                 MOD_DEC_USE_COUNT;
133                 GOTO(out_sem, rc);
134         }
135         cli->cl_conn_count++;
136         if (cli->cl_conn_count > 1)
137                 GOTO(out_sem, rc);
138
139         obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
140                                                 LDLM_NAMESPACE_CLIENT);
141         if (obd->obd_namespace == NULL)
142                 GOTO(out_disco, rc = -ENOMEM);
143
144         request = ptlrpc_prep_req(&cli->cl_import, rq_opc, 2, size, tmp);
145         if (!request)
146                 GOTO(out_ldlm, rc = -ENOMEM);
147
148         request->rq_level = LUSTRE_CONN_NEW;
149         request->rq_replen = lustre_msg_size(0, NULL);
150         request->rq_reqmsg->addr = conn->addr;
151         request->rq_reqmsg->cookie = conn->cookie;
152         c = class_conn2export(conn)->exp_connection =
153                 ptlrpc_connection_addref(request->rq_connection);
154         recovd_conn_manage(c, recovd, recover);
155
156         rc = ptlrpc_queue_wait(request);
157         rc = ptlrpc_check_status(request, rc);
158         if (rc)
159                 GOTO(out_req, rc);
160
161         if (rq_opc == MDS_CONNECT)
162                 cli->cl_import.imp_flags |= IMP_REPLAYABLE;
163         list_add(&cli->cl_import.imp_chain, &c->c_imports);
164         c->c_level = LUSTRE_CONN_FULL;
165         cli->cl_import.imp_handle.addr = request->rq_repmsg->addr;
166         cli->cl_import.imp_handle.cookie = request->rq_repmsg->cookie;
167
168         EXIT;
169 out_req:
170         ptlrpc_req_finished(request);
171         if (rc) {
172 out_ldlm:
173                 ldlm_namespace_free(obd->obd_namespace);
174                 obd->obd_namespace = NULL;
175 out_disco:
176                 class_disconnect(conn);
177                 MOD_DEC_USE_COUNT;
178         }
179 out_sem:
180         up(&cli->cl_sem);
181         return rc;
182 }
183
184 int client_obd_disconnect(struct lustre_handle *conn)
185 {
186         struct obd_device *obd = class_conn2obd(conn);
187         struct client_obd *cli = &obd->u.cli;
188         int rq_opc;
189         struct ptlrpc_request *request = NULL;
190         int rc, err;
191         ENTRY;
192
193         if (!obd) {
194                 CERROR("invalid connection for disconnect: addr "LPX64
195                        ", cookie "LPX64"\n", conn ? conn->addr : -1UL,
196                        conn ? conn->cookie : -1UL);
197                 RETURN(-EINVAL);
198         }
199
200         rq_opc = obd->obd_type->typ_ops->o_brw ? OST_DISCONNECT:MDS_DISCONNECT;
201         down(&cli->cl_sem);
202         if (!cli->cl_conn_count) {
203                 CERROR("disconnecting disconnected device (%s)\n",
204                        obd->obd_name);
205                 GOTO(out_sem, rc = -EINVAL);
206         }
207
208         cli->cl_conn_count--;
209         if (cli->cl_conn_count)
210                 GOTO(out_disco, rc = 0);
211
212         ldlm_namespace_free(obd->obd_namespace);
213         obd->obd_namespace = NULL;
214         request = ptlrpc_prep_req(&cli->cl_import, rq_opc, 0, NULL, NULL);
215         if (!request)
216                 GOTO(out_disco, rc = -ENOMEM);
217
218         request->rq_replen = lustre_msg_size(0, NULL);
219
220         rc = ptlrpc_queue_wait(request);
221         if (rc)
222                 GOTO(out_req, rc);
223
224         EXIT;
225  out_req:
226         if (request)
227                 ptlrpc_req_finished(request);
228  out_disco:
229         err = class_disconnect(conn);
230         if (!rc && err)
231                 rc = err;
232         list_del_init(&cli->cl_import.imp_chain);
233         MOD_DEC_USE_COUNT;
234  out_sem:
235         up(&cli->cl_sem);
236         RETURN(rc);
237 }