4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 021110-1307, USA
24 * Copyright (c) 2013, Intel Corporation.
25 * Use is subject to license terms.
27 * lustre/osp/lwp_dev.c
29 * Light Weight Proxy, which is just for managing the connection established
30 * from OSTs/MDTs to MDT0.
32 * Author: <di.wang@intel.com>
33 * Author: <yawei.niu@intel.com>
35 #define DEBUG_SUBSYSTEM S_OST
37 #include <obd_class.h>
38 #include <lustre_param.h>
39 #include <lustre_log.h>
40 #include <libcfs/libcfs_string.h>
43 struct lu_device lpd_dev;
44 struct obd_device *lpd_obd;
45 struct obd_uuid lpd_cluuid;
46 struct obd_export *lpd_exp;
50 static inline struct lwp_device *lu2lwp_dev(struct lu_device *d)
52 return container_of0(d, struct lwp_device, lpd_dev);
55 static inline struct lu_device *lwp2lu_dev(struct lwp_device *d)
60 static int lwp_setup(const struct lu_env *env, struct lwp_device *lwp,
63 struct lustre_cfg_bufs *bufs = NULL;
64 struct lustre_cfg *lcfg = NULL;
65 char *lwp_name = lwp->lpd_obd->obd_name;
66 char *server_uuid = NULL;
69 struct obd_import *imp;
70 int len = strlen(lwp_name) + 1;
78 OBD_ALLOC(server_uuid, len);
79 if (server_uuid == NULL)
80 GOTO(out, rc = -ENOMEM);
82 snprintf(server_uuid, len, "-%s-", LUSTRE_LWP_NAME);
83 ptr = cfs_strrstr(lwp_name, server_uuid);
85 CERROR("%s: failed to get server_uuid from lwp_name: rc = %d\n",
87 GOTO(out, rc = -EINVAL);
90 strncpy(server_uuid, lwp_name, ptr - lwp_name);
91 server_uuid[ptr - lwp_name] = '\0';
92 strlcat(server_uuid, "_UUID", len);
93 lustre_cfg_bufs_reset(bufs, lwp_name);
94 lustre_cfg_bufs_set_string(bufs, 1, server_uuid);
95 lustre_cfg_bufs_set_string(bufs, 2, nidstring);
96 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
98 GOTO(out, rc = -ENOMEM);
100 rc = client_obd_setup(lwp->lpd_obd, lcfg);
102 CERROR("%s: client obd setup error: rc = %d\n",
103 lwp->lpd_obd->obd_name, rc);
107 imp = lwp->lpd_obd->u.cli.cl_import;
108 rc = ptlrpc_init_import(imp);
112 ll_generate_random_uuid(uuid);
113 class_uuid_unparse(uuid, &lwp->lpd_cluuid);
117 if (server_uuid != NULL)
118 OBD_FREE(server_uuid, len);
120 lustre_cfg_free(lcfg);
122 client_obd_cleanup(lwp->lpd_obd);
127 static int lwp_disconnect(struct lwp_device *d)
129 struct obd_import *imp;
132 imp = d->lpd_obd->u.cli.cl_import;
134 /* Mark import deactivated now, so we don't try to reconnect if any
135 * of the cleanup RPCs fails (e.g. ldlm cancel, etc). We don't
136 * fully deactivate the import, or that would drop all requests. */
137 LASSERT(imp != NULL);
138 spin_lock(&imp->imp_lock);
139 imp->imp_deactive = 1;
140 spin_unlock(&imp->imp_lock);
142 ptlrpc_deactivate_import(imp);
144 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
145 * delete it regardless. (It's safe to delete an import that was
147 ptlrpc_pinger_del_import(imp);
148 rc = ptlrpc_disconnect_import(imp, 0);
150 CWARN("%s: can't disconnect: rc = %d\n",
151 d->lpd_obd->obd_name, rc);
153 ptlrpc_invalidate_import(imp);
158 static int lwp_process_config(const struct lu_env *env,
159 struct lu_device *dev, struct lustre_cfg *lcfg)
161 struct lwp_device *d = lu2lwp_dev(dev);
165 switch (lcfg->lcfg_command) {
166 case LCFG_PRE_CLEANUP:
168 rc = lwp_disconnect(d);
174 CERROR("%s: unknown command %u\n",
175 (char *)lustre_cfg_string(lcfg, 0), lcfg->lcfg_command);
183 const struct lu_device_operations lwp_lu_ops = {
184 .ldo_process_config = lwp_process_config,
187 int lwp_init0(const struct lu_env *env, struct lwp_device *lwp,
188 struct lu_device_type *ldt, struct lustre_cfg *cfg)
193 lwp->lpd_obd = class_name2obd(lustre_cfg_string(cfg, 0));
194 if (lwp->lpd_obd == NULL) {
195 CERROR("Cannot find obd with name %s\n",
196 lustre_cfg_string(cfg, 0));
200 lwp->lpd_dev.ld_ops = &lwp_lu_ops;
201 lwp->lpd_obd->obd_lu_dev = &lwp->lpd_dev;
203 rc = ptlrpcd_addref();
205 CERROR("%s: ptlrpcd addref error: rc =%d\n",
206 lwp->lpd_obd->obd_name, rc);
210 rc = lwp_setup(env, lwp, lustre_cfg_string(cfg, 1));
212 CERROR("%s: setup lwp failed. %d\n",
213 lwp->lpd_obd->obd_name, rc);
218 if (lprocfs_seq_obd_setup(lwp->lpd_obd) == 0)
219 ptlrpc_lprocfs_register_obd(lwp->lpd_obd);
224 static struct lu_device *lwp_device_free(const struct lu_env *env,
225 struct lu_device *lu)
227 struct lwp_device *m = lu2lwp_dev(lu);
230 if (atomic_read(&lu->ld_ref) && lu->ld_site) {
231 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
232 lu_site_print(env, lu->ld_site, &msgdata, lu_cdebug_printer);
234 lu_device_fini(&m->lpd_dev);
239 static struct lu_device *lwp_device_alloc(const struct lu_env *env,
240 struct lu_device_type *t,
241 struct lustre_cfg *lcfg)
243 struct lwp_device *lwp;
248 l = ERR_PTR(-ENOMEM);
253 lu_device_init(&lwp->lpd_dev, t);
254 rc = lwp_init0(env, lwp, t, lcfg);
256 lwp_device_free(env, l);
264 static struct lu_device *lwp_device_fini(const struct lu_env *env,
267 struct lwp_device *m = lu2lwp_dev(d);
268 struct obd_import *imp;
272 if (m->lpd_exp != NULL)
273 class_disconnect(m->lpd_exp);
275 imp = m->lpd_obd->u.cli.cl_import;
277 if (imp->imp_rq_pool) {
278 ptlrpc_free_rq_pool(imp->imp_rq_pool);
279 imp->imp_rq_pool = NULL;
283 ptlrpc_lprocfs_unregister_obd(m->lpd_obd);
284 lprocfs_obd_cleanup(m->lpd_obd);
286 rc = client_obd_cleanup(m->lpd_obd);
287 LASSERTF(rc == 0, "error %d\n", rc);
294 static struct lu_device_type_operations lwp_device_type_ops = {
295 .ldto_device_alloc = lwp_device_alloc,
296 .ldto_device_free = lwp_device_free,
297 .ldto_device_fini = lwp_device_fini
300 struct lu_device_type lwp_device_type = {
301 .ldt_tags = LU_DEVICE_DT,
302 .ldt_name = LUSTRE_LWP_NAME,
303 .ldt_ops = &lwp_device_type_ops,
304 .ldt_ctx_tags = LCT_MD_THREAD
307 static int lwp_obd_connect(const struct lu_env *env, struct obd_export **exp,
308 struct obd_device *obd, struct obd_uuid *cluuid,
309 struct obd_connect_data *data, void *localdata)
311 struct lwp_device *lwp = lu2lwp_dev(obd->obd_lu_dev);
312 struct client_obd *cli = &lwp->lpd_obd->u.cli;
313 struct obd_import *imp = cli->cl_import;
314 struct obd_connect_data *ocd;
315 struct lustre_handle conn;
320 CDEBUG(D_CONFIG, "connect #%d\n", lwp->lpd_connects);
323 down_write(&cli->cl_sem);
324 rc = class_connect(&conn, obd, cluuid);
328 *exp = class_conn2export(&conn);
331 /* Why should there ever be more than 1 connect? */
333 LASSERT(lwp->lpd_connects == 1);
335 imp->imp_dlm_handle = conn;
336 rc = ptlrpc_init_import(imp);
340 LASSERT(data != NULL);
341 ocd = &imp->imp_connect_data;
344 LASSERT(ocd->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT);
346 ocd->ocd_version = LUSTRE_VERSION_CODE;
347 imp->imp_connect_flags_orig = ocd->ocd_connect_flags;
349 rc = ptlrpc_connect_import(imp);
351 CERROR("%s: can't connect obd: rc = %d\n", obd->obd_name, rc);
355 ptlrpc_pinger_add_import(imp);
357 GOTO(out_dis, rc = 0);
361 class_disconnect(*exp);
367 up_write(&cli->cl_sem);
372 static int lwp_obd_disconnect(struct obd_export *exp)
374 struct obd_device *obd = exp->exp_obd;
375 struct lwp_device *lwp = lu2lwp_dev(obd->obd_lu_dev);
379 /* Only disconnect the underlying layers on the final disconnect. */
380 LASSERT(lwp->lpd_connects == 1);
383 rc = class_disconnect(exp);
385 CERROR("%s: class disconnect error: rc = %d\n",
391 static int lwp_import_event(struct obd_device *obd, struct obd_import *imp,
392 enum obd_import_event event)
395 case IMP_EVENT_DISCON:
396 case IMP_EVENT_INACTIVE:
397 case IMP_EVENT_ACTIVE:
399 case IMP_EVENT_INVALIDATE:
400 if (obd->obd_namespace == NULL)
402 ldlm_namespace_cleanup(obd->obd_namespace, LDLM_FL_LOCAL_ONLY);
407 CERROR("%s: unsupported import event: %#x\n",
408 obd->obd_name, event);
413 struct obd_ops lwp_obd_device_ops = {
414 .o_owner = THIS_MODULE,
415 .o_add_conn = client_import_add_conn,
416 .o_del_conn = client_import_del_conn,
417 .o_connect = lwp_obd_connect,
418 .o_disconnect = lwp_obd_disconnect,
419 .o_import_event = lwp_import_event,