1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5 * Author: Zach Brown <zab@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
23 #include <linux/version.h>
24 #include <linux/module.h>
27 #define DEBUG_SUBSYSTEM S_PTLBD
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>
35 static int ptlbd_cl_attach(struct obd_device *obd, obd_count len, void *buf)
37 struct lprocfs_static_vars lvars;
39 lprocfs_init_vars(ptlbd_cl, &lvars);
40 return lprocfs_obd_attach(obd, lvars.obd_vars);
43 static int ptlbd_cl_detach(struct obd_device *obd)
45 return lprocfs_obd_detach(obd);
48 static int ptlbd_cl_setup(struct obd_device *obd, obd_count len, void *buf)
50 struct ptlbd_obd *ptlbd = &obd->u.ptlbd;
51 struct lustre_cfg* lcfg = buf;
52 struct obd_import *imp;
55 if (ptlbd->bd_import != NULL)
58 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
59 CERROR("requires a PTLBD server UUID\n");
63 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
64 CERROR("PTLBD server UUID must be less than 38 characters\n");
68 obd_str2uuid(&ptlbd->bd_server_uuid, lustre_cfg_string(lcfg, 1));
71 * from client_obd_connect.. *shrug*
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);
80 imp->imp_state = LUSTRE_IMP_FULL;
82 ptlrpc_init_client(PTLBD_REQUEST_PORTAL, PTLBD_REPLY_PORTAL,
83 "ptlbd", &ptlbd->bd_client);
84 imp->imp_client = &ptlbd->bd_client;
86 memcpy(imp->imp_target_uuid.uuid, lustre_cfg_string(lcfg, 1),
87 LUSTRE_CFG_BUFLEN(lcfg, 1));
88 ptlbd_blk_register(ptlbd);
93 static int ptlbd_cl_cleanup(struct obd_device *obd, int flags)
95 struct ptlbd_obd *ptlbd = &obd->u.ptlbd;
96 struct obd_import *imp;
99 if ((!ptlbd) || (!(imp = ptlbd->bd_import)))
102 if (!imp->imp_connection)
105 ptlrpc_cleanup_client(imp);
106 ptlrpc_put_connection(imp->imp_connection);
108 class_destroy_import(imp);
109 class_import_put(imp);
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)
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),
125 char *tmp[] = {imp->imp_target_uuid.uuid,
130 if (!conn || !obd || !target_uuid)
133 rc = class_connect(conn, obd, target_uuid);
136 exp = class_conn2export(conn);
138 request = ptlrpc_prep_req(imp, LUSTRE_PBD_VERSION, PTLBD_CONNECT,
141 GOTO(out_disco, rc = -ENOMEM);
143 request->rq_send_state = LUSTRE_IMP_NEW;
144 request->rq_replen = lustre_msg_size(0, NULL);
146 imp->imp_dlm_handle = *conn;
148 imp->imp_state = LUSTRE_IMP_NEW;
149 rc = ptlrpc_queue_wait(request);
153 exp->exp_connection = ptlrpc_connection_addref(imp->imp_connection);
155 imp->imp_state = LUSTRE_IMP_FULL;
156 imp->imp_remote_handle = request->rq_repmsg->handle;
159 ptlrpc_req_finished(request);
162 class_disconnect(exp, 0);
163 class_export_put(exp);
168 /* modelled after ptlrpc_import_disconnect() */
169 int ptlbd_cl_disconnect(struct obd_export *exp, unsigned long flags)
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;
181 request = ptlrpc_prep_req(imp, LUSTRE_PBD_VERSION,
185 GOTO(out_req, rc = -ENOMEM);
187 request->rq_replen = lustre_msg_size(0, NULL);
188 request->rq_send_state = LUSTRE_IMP_FULL;
190 rc = ptlrpc_queue_wait(request);
194 ptlrpc_req_finished(request);
195 err = class_disconnect(exp, 0);
196 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
203 static struct obd_ops ptlbd_cl_obd_ops = {
204 .o_owner = THIS_MODULE,
205 .o_attach = ptlbd_cl_attach,
206 .o_detach = ptlbd_cl_detach,
207 .o_setup = ptlbd_cl_setup,
208 .o_cleanup = ptlbd_cl_cleanup,
209 .o_connect = ptlbd_cl_connect,
210 .o_disconnect = ptlbd_cl_disconnect,
213 static struct lprocfs_vars lprocfs_obd_vars[] = { {0} };
214 static struct lprocfs_vars lprocfs_module_vars[] = { {0} };
215 LPROCFS_INIT_VARS(ptlbd_cl, lprocfs_module_vars, lprocfs_obd_vars)
217 int ptlbd_cl_init(void)
219 struct lprocfs_static_vars lvars;
221 lprocfs_init_vars(ptlbd_cl,&lvars);
222 return class_register_type(&ptlbd_cl_obd_ops, NULL, lvars.module_vars,
223 OBD_PTLBD_CL_DEVICENAME);
226 void ptlbd_cl_exit(void)
228 class_unregister_type(OBD_PTLBD_CL_DEVICENAME);
231 int ptlbd_do_connect(struct ptlbd_obd *ptlbd)
234 struct obd_device *obd = ptlbd->bd_import->imp_obd;
235 struct lustre_handle conn;
238 memset(&conn, 0, sizeof(conn));
239 rc = obd_connect(&conn, obd, &ptlbd->bd_server_uuid, NULL, 0);
242 ptlbd->bd_exp = class_conn2export(&conn);
247 int ptlbd_do_disconnect(struct ptlbd_obd *ptlbd)
252 rc = obd_disconnect(ptlbd->bd_exp, 0);