Whamcloud - gitweb
- landing of b_fid after merge with b_hd_cleanup_merge.
[fs/lustre-release.git] / lustre / ptlbd / client.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5  *   Author: Zach Brown <zab@clusterfs.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #include <linux/version.h>
24 #include <linux/module.h>
25 #include <linux/fs.h>
26
27 #define DEBUG_SUBSYSTEM S_PTLBD
28
29 #include <linux/obd_support.h>
30 #include <linux/obd_class.h>
31 #include <linux/lustre_debug.h>
32 #include <linux/lprocfs_status.h>
33 #include <linux/obd_ptlbd.h>
34
35 static int ptlbd_cl_attach(struct obd_device *obd, obd_count len, void *buf)
36 {
37         struct lprocfs_static_vars lvars;
38
39         lprocfs_init_vars(ptlbd_cl, &lvars);
40         return lprocfs_obd_attach(obd, lvars.obd_vars);
41 }
42
43 static int ptlbd_cl_detach(struct obd_device *obd)
44 {
45         return lprocfs_obd_detach(obd);
46 }
47
48 static int ptlbd_cl_setup(struct obd_device *obd, obd_count len, void *buf)
49 {
50         struct ptlbd_obd *ptlbd = &obd->u.ptlbd;
51         struct lustre_cfg* lcfg = buf;
52         struct obd_import *imp;
53         ENTRY;
54
55         if (ptlbd->bd_import != NULL)
56                 RETURN(-EALREADY);
57
58         if (lcfg->lcfg_inllen1 < 1) {
59                 CERROR("requires a PTLBD server UUID\n");
60                 RETURN(-EINVAL);
61         }
62
63         if (lcfg->lcfg_inllen1 > 37) {
64                 CERROR("PTLBD server UUID must be less than 38 characters\n");
65                 RETURN(-EINVAL);
66         }
67
68         obd_str2uuid(&ptlbd->bd_server_uuid, lcfg->lcfg_inlbuf1);
69
70         /*
71          * from client_obd_connect.. *shrug*
72          */
73         imp = ptlbd->bd_import = class_new_import();
74         imp->imp_connection = ptlrpc_uuid_to_connection(&ptlbd->bd_server_uuid);
75         if (!imp->imp_connection) {
76                 class_destroy_import(imp);
77                 class_import_put(imp);
78                 RETURN(-ENOENT);
79         }
80         imp->imp_state = LUSTRE_IMP_FULL;
81
82         ptlrpc_init_client(PTLBD_REQUEST_PORTAL, PTLBD_REPLY_PORTAL,
83                         "ptlbd", &ptlbd->bd_client);
84         imp->imp_client = &ptlbd->bd_client;
85         imp->imp_obd = obd;
86         memcpy(imp->imp_target_uuid.uuid, lcfg->lcfg_inlbuf1,
87                lcfg->lcfg_inllen1);
88         ptlbd_blk_register(ptlbd);
89
90         RETURN(0);
91 }
92
93 static int ptlbd_cl_cleanup(struct obd_device *obd, int flags)
94 {
95         struct ptlbd_obd *ptlbd = &obd->u.ptlbd;
96         struct obd_import *imp;
97         ENTRY;
98
99         if ((!ptlbd) || (!(imp = ptlbd->bd_import)))
100                 RETURN(-ENOENT);
101
102         if (!imp->imp_connection)
103                 RETURN(-ENOENT);
104
105         ptlrpc_cleanup_client(imp);
106         ptlrpc_put_connection(imp->imp_connection);
107
108         class_destroy_import(imp);
109         class_import_put(imp);
110
111         RETURN(0);
112 }
113
114 /* modelled after ptlrpc_import_connect() */
115 int ptlbd_cl_connect(struct lustre_handle *conn, struct obd_device *obd,
116                      struct obd_uuid *target_uuid, unsigned long connect_flags)
117 {
118         struct ptlbd_obd *ptlbd = &obd->u.ptlbd;
119         struct obd_import *imp = ptlbd->bd_import;
120         struct obd_export *exp;
121         struct ptlrpc_request *request;
122         int     rc, size[] = {sizeof(imp->imp_target_uuid),
123                               sizeof(obd->obd_uuid),
124                               sizeof(*conn)};
125         char *tmp[] = {imp->imp_target_uuid.uuid,
126                        obd->obd_uuid.uuid,
127                        (char*)conn};
128         ENTRY;
129
130         if (!conn || !obd || !target_uuid)
131                 RETURN(-EINVAL);
132
133         rc = class_connect(conn, obd, target_uuid);
134         if (rc)
135                 RETURN(rc);
136         exp = class_conn2export(conn);
137
138         request = ptlrpc_prep_req(imp, LUSTRE_PBD_VERSION, PTLBD_CONNECT,
139                                   3, size, tmp);
140         if (!request)
141                 GOTO(out_disco, rc = -ENOMEM);
142
143         request->rq_send_state = LUSTRE_IMP_NEW;
144         request->rq_replen = lustre_msg_size(0, NULL);
145
146         imp->imp_dlm_handle = *conn;
147
148         imp->imp_state = LUSTRE_IMP_NEW;
149         rc = ptlrpc_queue_wait(request);
150         if (rc)
151                 GOTO(out_req, rc);
152
153         exp->exp_connection = ptlrpc_connection_addref(imp->imp_connection);
154
155         imp->imp_state = LUSTRE_IMP_FULL;
156         imp->imp_remote_handle = request->rq_repmsg->handle;
157
158 out_req:
159         ptlrpc_req_finished(request);
160 out_disco:
161         if (rc)
162                 class_disconnect(exp, 0);
163         class_export_put(exp);
164         RETURN(rc);
165 }
166
167
168 /* modelled after ptlrpc_import_disconnect() */
169 int ptlbd_cl_disconnect(struct obd_export *exp, int flags)
170 {
171         struct obd_device *obd = exp->exp_obd;
172         struct ptlbd_obd *ptlbd = &obd->u.ptlbd;
173         struct obd_import *imp = ptlbd->bd_import;
174         struct ptlrpc_request *request;
175         int rc, err;
176         ENTRY;
177
178         if (!obd)
179                 RETURN(-EINVAL);
180
181         request = ptlrpc_prep_req(imp, LUSTRE_PBD_VERSION, PTLBD_DISCONNECT,
182                                   0, NULL, NULL);
183         if (!request)
184                 GOTO(out_req, rc = -ENOMEM);
185
186         request->rq_replen = lustre_msg_size(0, NULL);
187         request->rq_send_state = LUSTRE_IMP_FULL;
188
189         rc = ptlrpc_queue_wait(request);
190
191 out_req:
192         if (request)
193                 ptlrpc_req_finished(request);
194         err = class_disconnect(exp, 0);
195         memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
196         if (!rc && err)
197                 rc = err;
198         RETURN(rc);
199 }
200
201
202 static struct obd_ops ptlbd_cl_obd_ops = {
203         .o_owner        = THIS_MODULE,
204         .o_attach       = ptlbd_cl_attach,
205         .o_detach       = ptlbd_cl_detach,
206         .o_setup        = ptlbd_cl_setup,
207         .o_cleanup      = ptlbd_cl_cleanup,
208         .o_connect      = ptlbd_cl_connect,
209         .o_disconnect   = ptlbd_cl_disconnect,
210 };
211
212 static struct lprocfs_vars lprocfs_obd_vars[] = { {0} };
213 static struct lprocfs_vars lprocfs_module_vars[] = { {0} };
214 LPROCFS_INIT_VARS(ptlbd_cl, lprocfs_module_vars, lprocfs_obd_vars)
215
216 int ptlbd_cl_init(void)
217 {
218         struct lprocfs_static_vars lvars;
219
220         lprocfs_init_vars(ptlbd_cl,&lvars);
221         return class_register_type(&ptlbd_cl_obd_ops, NULL, lvars.module_vars,
222                                    OBD_PTLBD_CL_DEVICENAME);
223 }
224
225 void ptlbd_cl_exit(void)
226 {
227         class_unregister_type(OBD_PTLBD_CL_DEVICENAME);
228 }
229
230 int ptlbd_do_connect(struct ptlbd_obd *ptlbd)
231 {
232         int     rc;
233         struct obd_device       *obd = ptlbd->bd_import->imp_obd;
234         struct lustre_handle conn;
235         ENTRY;
236
237         memset(&conn, 0, sizeof(conn));
238         rc = obd_connect(&conn, obd, &ptlbd->bd_server_uuid, 0);
239         if (rc < 0)
240                 RETURN(rc);
241         ptlbd->bd_exp = class_conn2export(&conn);
242         RETURN(rc);
243 }
244
245
246 int ptlbd_do_disconnect(struct ptlbd_obd *ptlbd)
247 {
248         int     rc;
249         ENTRY;
250
251         rc = obd_disconnect(ptlbd->bd_exp, 0);
252         RETURN(rc);
253 }
254