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>
42 struct lu_device lpd_dev;
43 struct obd_device *lpd_obd;
44 struct obd_uuid lpd_cluuid;
45 struct obd_export *lpd_exp;
49 static inline struct lwp_device *lu2lwp_dev(struct lu_device *d)
51 return container_of0(d, struct lwp_device, lpd_dev);
54 static inline struct lu_device *lwp2lu_dev(struct lwp_device *d)
59 static int lwp_name2fsname(char *lwpname, char *fsname)
63 LASSERT(lwpname != NULL);
64 LASSERT(fsname != NULL);
66 sprintf(fsname, "-%s-", LUSTRE_LWP_NAME);
68 ptr = strstr(lwpname, fsname);
72 while (*(--ptr) != '-') {
77 strncpy(fsname, lwpname, ptr - lwpname);
78 fsname[ptr - lwpname] = '\0';
83 static int lwp_setup(const struct lu_env *env, struct lwp_device *lwp,
86 struct lustre_cfg_bufs *bufs = NULL;
87 struct lustre_cfg *lcfg = NULL;
88 char *lwpname = lwp->lpd_obd->obd_name;
90 char *server_uuid = NULL;
92 struct obd_import *imp;
100 OBD_ALLOC(fsname, strlen(lwpname));
102 GOTO(out, rc = -ENOMEM);
104 rc = lwp_name2fsname(lwpname, fsname);
106 CERROR("%s: failed to get fsname from lwpname. %d\n",
111 OBD_ALLOC(server_uuid, strlen(fsname) + 15);
112 if (server_uuid == NULL)
113 GOTO(out, rc = -ENOMEM);
115 sprintf(server_uuid, "%s-MDT0000_UUID", fsname);
116 lustre_cfg_bufs_reset(bufs, lwpname);
117 lustre_cfg_bufs_set_string(bufs, 1, server_uuid);
118 lustre_cfg_bufs_set_string(bufs, 2, nidstring);
119 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
121 GOTO(out, rc = -ENOMEM);
123 rc = client_obd_setup(lwp->lpd_obd, lcfg);
125 CERROR("%s: client obd setup error: rc = %d\n",
126 lwp->lpd_obd->obd_name, rc);
130 imp = lwp->lpd_obd->u.cli.cl_import;
131 rc = ptlrpc_init_import(imp);
135 ll_generate_random_uuid(uuid);
136 class_uuid_unparse(uuid, &lwp->lpd_cluuid);
140 if (server_uuid != NULL)
141 OBD_FREE(server_uuid, strlen(fsname) + 15);
143 OBD_FREE(fsname, strlen(lwpname));
145 lustre_cfg_free(lcfg);
147 client_obd_cleanup(lwp->lpd_obd);
152 int lwp_disconnect(struct lwp_device *d)
154 struct obd_import *imp;
157 imp = d->lpd_obd->u.cli.cl_import;
159 /* Mark import deactivated now, so we don't try to reconnect if any
160 * of the cleanup RPCs fails (e.g. ldlm cancel, etc). We don't
161 * fully deactivate the import, or that would drop all requests. */
162 LASSERT(imp != NULL);
163 spin_lock(&imp->imp_lock);
164 imp->imp_deactive = 1;
165 spin_unlock(&imp->imp_lock);
167 ptlrpc_deactivate_import(imp);
169 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
170 * delete it regardless. (It's safe to delete an import that was
172 (void)ptlrpc_pinger_del_import(imp);
174 rc = ptlrpc_disconnect_import(imp, 0);
175 if (rc && rc != -ETIMEDOUT)
176 CERROR("%s: can't disconnect: rc = %d\n",
177 d->lpd_obd->obd_name, rc);
179 ptlrpc_invalidate_import(imp);
184 static int lwp_process_config(const struct lu_env *env,
185 struct lu_device *dev, struct lustre_cfg *lcfg)
187 struct lwp_device *d = lu2lwp_dev(dev);
191 switch (lcfg->lcfg_command) {
192 case LCFG_PRE_CLEANUP:
194 rc = lwp_disconnect(d);
200 CERROR("%s: unknown command %u\n",
201 (char *)lustre_cfg_string(lcfg, 0), lcfg->lcfg_command);
209 const struct lu_device_operations lwp_lu_ops = {
210 .ldo_process_config = lwp_process_config,
213 static struct lprocfs_vars lprocfs_lwp_module_vars[] = {
214 { "num_refs", lprocfs_rd_numrefs, 0, 0 },
218 static struct lprocfs_vars lprocfs_lwp_obd_vars[] = {
222 void lprocfs_lwp_init_vars(struct lprocfs_static_vars *lvars)
224 lvars->module_vars = lprocfs_lwp_module_vars;
225 lvars->obd_vars = lprocfs_lwp_obd_vars;
228 int lwp_init0(const struct lu_env *env, struct lwp_device *lwp,
229 struct lu_device_type *ldt, struct lustre_cfg *cfg)
231 struct lprocfs_static_vars lvars = { 0 };
235 lwp->lpd_obd = class_name2obd(lustre_cfg_string(cfg, 0));
236 if (lwp->lpd_obd == NULL) {
237 CERROR("Cannot find obd with name %s\n",
238 lustre_cfg_string(cfg, 0));
242 lwp->lpd_dev.ld_ops = &lwp_lu_ops;
243 lwp->lpd_obd->obd_lu_dev = &lwp->lpd_dev;
245 rc = ptlrpcd_addref();
247 CERROR("%s: ptlrpcd addref error: rc =%d\n",
248 lwp->lpd_obd->obd_name, rc);
252 rc = lwp_setup(env, lwp, lustre_cfg_string(cfg, 1));
254 CERROR("%s: setup lwp failed. %d\n",
255 lwp->lpd_obd->obd_name, rc);
260 lprocfs_lwp_init_vars(&lvars);
261 if (lprocfs_obd_setup(lwp->lpd_obd, lvars.obd_vars) == 0)
262 ptlrpc_lprocfs_register_obd(lwp->lpd_obd);
267 static struct lu_device *lwp_device_free(const struct lu_env *env,
268 struct lu_device *lu)
270 struct lwp_device *m = lu2lwp_dev(lu);
273 if (cfs_atomic_read(&lu->ld_ref) && lu->ld_site) {
274 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
275 lu_site_print(env, lu->ld_site, &msgdata, lu_cdebug_printer);
277 lu_device_fini(&m->lpd_dev);
282 static struct lu_device *lwp_device_alloc(const struct lu_env *env,
283 struct lu_device_type *t,
284 struct lustre_cfg *lcfg)
286 struct lwp_device *lwp;
291 l = ERR_PTR(-ENOMEM);
296 lu_device_init(&lwp->lpd_dev, t);
297 rc = lwp_init0(env, lwp, t, lcfg);
299 lwp_device_free(env, l);
307 static struct lu_device *lwp_device_fini(const struct lu_env *env,
310 struct lwp_device *m = lu2lwp_dev(d);
311 struct obd_import *imp;
315 if (m->lpd_exp != NULL)
316 class_disconnect(m->lpd_exp);
318 imp = m->lpd_obd->u.cli.cl_import;
320 if (imp->imp_rq_pool) {
321 ptlrpc_free_rq_pool(imp->imp_rq_pool);
322 imp->imp_rq_pool = NULL;
326 ptlrpc_lprocfs_unregister_obd(m->lpd_obd);
327 lprocfs_obd_cleanup(m->lpd_obd);
329 rc = client_obd_cleanup(m->lpd_obd);
330 LASSERTF(rc == 0, "error %d\n", rc);
337 static struct lu_device_type_operations lwp_device_type_ops = {
338 .ldto_device_alloc = lwp_device_alloc,
339 .ldto_device_free = lwp_device_free,
340 .ldto_device_fini = lwp_device_fini
343 struct lu_device_type lwp_device_type = {
344 .ldt_tags = LU_DEVICE_DT,
345 .ldt_name = LUSTRE_LWP_NAME,
346 .ldt_ops = &lwp_device_type_ops,
347 .ldt_ctx_tags = LCT_MD_THREAD
350 static int lwp_obd_connect(const struct lu_env *env, struct obd_export **exp,
351 struct obd_device *obd, struct obd_uuid *cluuid,
352 struct obd_connect_data *data, void *localdata)
354 struct lwp_device *lwp = lu2lwp_dev(obd->obd_lu_dev);
355 struct obd_connect_data *ocd;
356 struct obd_import *imp;
357 struct lustre_handle conn;
362 CDEBUG(D_CONFIG, "connect #%d\n", lwp->lpd_connects);
364 rc = class_connect(&conn, obd, cluuid);
368 *exp = class_conn2export(&conn);
371 /* Why should there ever be more than 1 connect? */
373 LASSERT(lwp->lpd_connects == 1);
375 imp = lwp->lpd_obd->u.cli.cl_import;
376 imp->imp_dlm_handle = conn;
378 LASSERT(data != NULL);
379 ocd = &imp->imp_connect_data;
382 LASSERT(ocd->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT);
384 ocd->ocd_version = LUSTRE_VERSION_CODE;
385 imp->imp_connect_flags_orig = ocd->ocd_connect_flags;
387 rc = ptlrpc_connect_import(imp);
389 CERROR("%s: can't connect obd: rc = %d\n", obd->obd_name, rc);
393 ptlrpc_pinger_add_import(imp);
399 static int lwp_obd_disconnect(struct obd_export *exp)
401 struct obd_device *obd = exp->exp_obd;
402 struct lwp_device *lwp = lu2lwp_dev(obd->obd_lu_dev);
406 /* Only disconnect the underlying layers on the final disconnect. */
407 LASSERT(lwp->lpd_connects == 1);
410 rc = class_disconnect(exp);
412 CERROR("%s: class disconnect error: rc = %d\n",
418 static int lwp_import_event(struct obd_device *obd, struct obd_import *imp,
419 enum obd_import_event event)
422 case IMP_EVENT_DISCON:
423 case IMP_EVENT_INACTIVE:
424 case IMP_EVENT_ACTIVE:
426 case IMP_EVENT_INVALIDATE:
427 if (obd->obd_namespace == NULL)
429 ldlm_namespace_cleanup(obd->obd_namespace, LDLM_FL_LOCAL_ONLY);
434 CERROR("%s: unsupported import event: %#x\n",
435 obd->obd_name, event);
440 struct obd_ops lwp_obd_device_ops = {
441 .o_owner = THIS_MODULE,
442 .o_add_conn = client_import_add_conn,
443 .o_del_conn = client_import_del_conn,
444 .o_connect = lwp_obd_connect,
445 .o_disconnect = lwp_obd_disconnect,
446 .o_import_event = lwp_import_event,