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, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osp/osp_dev.c
38 * Lustre OST Proxy Device
40 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
41 * Author: Mikhail Pershin <mike.pershin@intel.com>
45 # define EXPORT_SYMTAB
47 #define DEBUG_SUBSYSTEM S_MDS
49 #include <obd_class.h>
50 #include <lustre_param.h>
52 #include "osp_internal.h"
54 /* Slab for OSP object allocation */
55 cfs_mem_cache_t *osp_object_kmem;
57 static struct lu_kmem_descr osp_caches[] = {
59 .ckd_cache = &osp_object_kmem,
60 .ckd_name = "osp_obj",
61 .ckd_size = sizeof(struct osp_object)
68 struct lu_object *osp_object_alloc(const struct lu_env *env,
69 const struct lu_object_header *hdr,
72 struct lu_object_header *h;
78 OBD_SLAB_ALLOC_PTR_GFP(o, osp_object_kmem, CFS_ALLOC_IO);
80 l = &o->opo_obj.do_lu;
83 lu_object_header_init(h);
84 dt_object_init(&o->opo_obj, h, d);
85 lu_object_add_top(h, l);
87 l->lo_ops = &osp_lu_obj_ops;
95 static int osp_shutdown(const struct lu_env *env, struct osp_device *d)
97 struct obd_import *imp;
102 imp = d->opd_obd->u.cli.cl_import;
104 /* Mark import deactivated now, so we don't try to reconnect if any
105 * of the cleanup RPCs fails (e.g. ldlm cancel, etc). We don't
106 * fully deactivate the import, or that would drop all requests. */
107 cfs_spin_lock(&imp->imp_lock);
108 imp->imp_deactive = 1;
109 cfs_spin_unlock(&imp->imp_lock);
111 ptlrpc_deactivate_import(imp);
113 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
114 * delete it regardless. (It's safe to delete an import that was
116 (void)ptlrpc_pinger_del_import(imp);
118 rc = ptlrpc_disconnect_import(imp, 0);
120 CERROR("%s: can't disconnect: rc = %d\n",
121 d->opd_obd->obd_name, rc);
123 ptlrpc_invalidate_import(imp);
128 static int osp_process_config(const struct lu_env *env,
129 struct lu_device *dev, struct lustre_cfg *lcfg)
131 struct osp_device *d = lu2osp_dev(dev);
132 struct lprocfs_static_vars lvars = { 0 };
137 switch (lcfg->lcfg_command) {
139 lu_dev_del_linkage(dev->ld_site, dev);
140 rc = osp_shutdown(env, d);
143 lprocfs_osp_init_vars(&lvars);
146 rc = class_process_proc_param(PARAM_OSP, lvars.obd_vars,
151 /* class_process_proc_param() haven't found matching
152 * parameter and returned ENOSYS so that layer(s)
153 * below could use that. But OSP is the bottom, so
155 CERROR("%s: unknown param %s\n",
156 (char *)lustre_cfg_string(lcfg, 0),
157 (char *)lustre_cfg_string(lcfg, 1));
162 CERROR("%s: unknown command %u\n",
163 (char *)lustre_cfg_string(lcfg, 0), lcfg->lcfg_command);
171 static int osp_recovery_complete(const struct lu_env *env,
172 struct lu_device *dev)
174 struct osp_device *osp = lu2osp_dev(dev);
178 osp->opd_recovery_completed = 1;
182 const struct lu_device_operations osp_lu_ops = {
183 .ldo_object_alloc = osp_object_alloc,
184 .ldo_process_config = osp_process_config,
185 .ldo_recovery_complete = osp_recovery_complete,
188 static int osp_sync(const struct lu_env *env, struct dt_device *dev)
193 * XXX: wake up sync thread, command it to start flushing asap?
199 static const struct dt_device_operations osp_dt_ops = {
203 static int osp_connect_to_osd(const struct lu_env *env, struct osp_device *m,
206 struct obd_connect_data *data = NULL;
207 struct obd_device *obd;
212 LASSERT(m->opd_storage_exp == NULL);
218 obd = class_name2obd(nextdev);
220 CERROR("%s: can't locate next device: %s\n",
221 m->opd_obd->obd_name, nextdev);
222 GOTO(out, rc = -ENOTCONN);
225 rc = obd_connect(env, &m->opd_storage_exp, obd, &obd->obd_uuid, data,
228 CERROR("%s: cannot connect to next dev %s: rc = %d\n",
229 m->opd_obd->obd_name, nextdev, rc);
233 m->opd_dt_dev.dd_lu_dev.ld_site =
234 m->opd_storage_exp->exp_obd->obd_lu_dev->ld_site;
235 LASSERT(m->opd_dt_dev.dd_lu_dev.ld_site);
236 m->opd_storage = lu2dt_dev(m->opd_storage_exp->exp_obd->obd_lu_dev);
243 static int osp_init0(const struct lu_env *env, struct osp_device *m,
244 struct lu_device_type *ldt, struct lustre_cfg *cfg)
246 struct lprocfs_static_vars lvars = { 0 };
247 struct proc_dir_entry *osc_proc_dir;
248 struct obd_import *imp;
250 char *src, *ost, *mdt, *osdname = NULL;
255 m->opd_obd = class_name2obd(lustre_cfg_string(cfg, 0));
256 if (m->opd_obd == NULL) {
257 CERROR("Cannot find obd with name %s\n",
258 lustre_cfg_string(cfg, 0));
262 /* There is no record in the MDT configuration for the local disk
263 * device, so we have to extract this from elsewhere in the profile.
264 * The only information we get at setup is from the OSC records:
265 * setup 0:{fsname}-OSTxxxx-osc[-MDTxxxx] 1:lustre-OST0000_UUID 2:NID
266 * Note that 1.8 generated configs are missing the -MDTxxxx part.
267 * We need to reconstruct the name of the underlying OSD from this:
268 * {fsname}-{svname}-osd, for example "lustre-MDT0000-osd". We
269 * also need to determine the OST index from this - will be used
270 * to calculate the offset in shared lov_objids file later */
272 src = lustre_cfg_string(cfg, 0);
276 ost = strstr(src, "-OST");
280 idx = simple_strtol(ost + 4, &mdt, 16);
281 if (mdt[0] != '-' || idx > INT_MAX || idx < 0) {
282 CERROR("%s: invalid OST index in '%s'\n",
283 m->opd_obd->obd_name, src);
284 GOTO(out_fini, rc = -EINVAL);
289 /* check the fsname length, and after this everything else will fit */
290 if (idx > MTI_NAME_MAXLEN) {
291 CERROR("%s: fsname too long in '%s'\n",
292 m->opd_obd->obd_name, src);
293 GOTO(out_fini, rc = -EINVAL);
296 OBD_ALLOC(osdname, MAX_OBD_NAME);
298 GOTO(out_fini, rc = -ENOMEM);
300 memcpy(osdname, src, idx); /* copy just the fsname part */
303 mdt = strstr(mdt, "-MDT");
304 if (mdt == NULL) /* 1.8 configs don't have "-MDT0000" at the end */
305 strcat(osdname, "-MDT0000");
307 strcat(osdname, mdt);
308 strcat(osdname, "-osd");
309 CDEBUG(D_HA, "%s: connect to %s (%s)\n",
310 m->opd_obd->obd_name, osdname, src);
312 m->opd_dt_dev.dd_lu_dev.ld_ops = &osp_lu_ops;
313 m->opd_dt_dev.dd_ops = &osp_dt_ops;
314 m->opd_obd->obd_lu_dev = &m->opd_dt_dev.dd_lu_dev;
316 rc = osp_connect_to_osd(env, m, osdname);
320 rc = ptlrpcd_addref();
322 GOTO(out_disconnect, rc);
324 rc = client_obd_setup(m->opd_obd, cfg);
326 CERROR("%s: can't setup obd: %d\n", m->opd_obd->obd_name, rc);
330 lprocfs_osp_init_vars(&lvars);
331 if (lprocfs_obd_setup(m->opd_obd, lvars.obd_vars) == 0)
332 ptlrpc_lprocfs_register_obd(m->opd_obd);
334 /* for compatibility we link old procfs's OSC entries to osp ones */
335 osc_proc_dir = lprocfs_srch(proc_lustre_root, "osc");
337 cfs_proc_dir_entry_t *symlink = NULL;
340 OBD_ALLOC(name, strlen(m->opd_obd->obd_name) + 1);
342 GOTO(out, rc = -ENOMEM);
344 strcpy(name, m->opd_obd->obd_name);
345 if (strstr(name, "osc"))
346 symlink = lprocfs_add_symlink(name, osc_proc_dir,
348 m->opd_obd->obd_name);
349 OBD_FREE(name, strlen(m->opd_obd->obd_name) + 1);
350 m->opd_symlink = symlink;
354 * Initiate connect to OST
356 ll_generate_random_uuid(uuid);
357 class_uuid_unparse(uuid, &m->opd_cluuid);
359 imp = m->opd_obd->u.cli.cl_import;
361 rc = ptlrpc_init_import(imp);
365 OBD_FREE(osdname, MAX_OBD_NAME);
369 ptlrpc_lprocfs_unregister_obd(m->opd_obd);
370 lprocfs_obd_cleanup(m->opd_obd);
371 class_destroy_import(m->opd_obd->u.cli.cl_import);
372 client_obd_cleanup(m->opd_obd);
376 obd_disconnect(m->opd_storage_exp);
379 OBD_FREE(osdname, MAX_OBD_NAME);
383 static struct lu_device *osp_device_free(const struct lu_env *env,
384 struct lu_device *lu)
386 struct osp_device *m = lu2osp_dev(lu);
390 dt_device_fini(&m->opd_dt_dev);
395 static struct lu_device *osp_device_alloc(const struct lu_env *env,
396 struct lu_device_type *t,
397 struct lustre_cfg *lcfg)
399 struct osp_device *m;
404 l = ERR_PTR(-ENOMEM);
409 dt_device_init(&m->opd_dt_dev, t);
410 rc = osp_init0(env, m, t, lcfg);
412 osp_device_free(env, l);
419 static struct lu_device *osp_device_fini(const struct lu_env *env,
422 struct osp_device *m = lu2osp_dev(d);
423 struct obd_import *imp;
428 LASSERT(m->opd_storage_exp);
429 obd_disconnect(m->opd_storage_exp);
431 imp = m->opd_obd->u.cli.cl_import;
433 if (imp->imp_rq_pool) {
434 ptlrpc_free_rq_pool(imp->imp_rq_pool);
435 imp->imp_rq_pool = NULL;
438 obd_cleanup_client_import(m->opd_obd);
441 lprocfs_remove(&m->opd_symlink);
444 ptlrpc_lprocfs_unregister_obd(m->opd_obd);
445 lprocfs_obd_cleanup(m->opd_obd);
447 rc = client_obd_cleanup(m->opd_obd);
448 LASSERTF(rc == 0, "error %d\n", rc);
455 static int osp_reconnect(const struct lu_env *env,
456 struct obd_export *exp, struct obd_device *obd,
457 struct obd_uuid *cluuid,
458 struct obd_connect_data *data,
465 * we use exports to track all LOD users
467 static int osp_obd_connect(const struct lu_env *env, struct obd_export **exp,
468 struct obd_device *obd, struct obd_uuid *cluuid,
469 struct obd_connect_data *data, void *localdata)
471 struct osp_device *osp = lu2osp_dev(obd->obd_lu_dev);
472 struct obd_connect_data *ocd;
473 struct obd_import *imp;
474 struct lustre_handle conn;
479 CDEBUG(D_CONFIG, "connect #%d\n", osp->opd_connects);
481 rc = class_connect(&conn, obd, cluuid);
485 *exp = class_conn2export(&conn);
487 /* Why should there ever be more than 1 connect? */
489 LASSERT(osp->opd_connects == 1);
491 imp = osp->opd_obd->u.cli.cl_import;
492 imp->imp_dlm_handle = conn;
494 ocd = &imp->imp_connect_data;
495 ocd->ocd_connect_flags = OBD_CONNECT_AT |
498 #ifdef HAVE_LRU_RESIZE_SUPPORT
499 OBD_CONNECT_LRU_RESIZE |
502 OBD_CONNECT_OSS_CAPA |
503 OBD_CONNECT_REQPORTAL |
504 OBD_CONNECT_SKIP_ORPHAN |
506 ocd->ocd_version = LUSTRE_VERSION_CODE;
507 LASSERT(data->ocd_connect_flags & OBD_CONNECT_INDEX);
508 ocd->ocd_index = data->ocd_index;
509 imp->imp_connect_flags_orig = ocd->ocd_connect_flags;
511 rc = ptlrpc_connect_import(imp);
513 CERROR("%s: can't connect obd: rc = %d\n", obd->obd_name, rc);
517 ptlrpc_pinger_add_import(imp);
524 * once last export (we don't count self-export) disappeared
525 * osp can be released
527 static int osp_obd_disconnect(struct obd_export *exp)
529 struct obd_device *obd = exp->exp_obd;
530 struct osp_device *osp = lu2osp_dev(obd->obd_lu_dev);
535 /* Only disconnect the underlying layers on the final disconnect. */
536 LASSERT(osp->opd_connects == 1);
539 rc = class_disconnect(exp);
541 /* destroy the device */
543 class_manual_cleanup(obd);
548 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
549 LU_KEY_INIT_FINI(osp, struct osp_thread_info);
550 static void osp_key_exit(const struct lu_context *ctx,
551 struct lu_context_key *key, void *data)
553 struct osp_thread_info *info = data;
555 info->osi_attr.la_valid = 0;
558 struct lu_context_key osp_thread_key = {
559 .lct_tags = LCT_MD_THREAD,
560 .lct_init = osp_key_init,
561 .lct_fini = osp_key_fini,
562 .lct_exit = osp_key_exit
565 /* context key constructor/destructor: mdt_txn_key_init, mdt_txn_key_fini */
566 LU_KEY_INIT_FINI(osp_txn, struct osp_txn_info);
568 struct lu_context_key osp_txn_key = {
569 .lct_tags = LCT_OSP_THREAD,
570 .lct_init = osp_txn_key_init,
571 .lct_fini = osp_txn_key_fini
573 LU_TYPE_INIT_FINI(osp, &osp_thread_key, &osp_txn_key);
575 static struct lu_device_type_operations osp_device_type_ops = {
576 .ldto_init = osp_type_init,
577 .ldto_fini = osp_type_fini,
579 .ldto_start = osp_type_start,
580 .ldto_stop = osp_type_stop,
582 .ldto_device_alloc = osp_device_alloc,
583 .ldto_device_free = osp_device_free,
585 .ldto_device_fini = osp_device_fini
588 static struct lu_device_type osp_device_type = {
589 .ldt_tags = LU_DEVICE_DT,
590 .ldt_name = LUSTRE_OSP_NAME,
591 .ldt_ops = &osp_device_type_ops,
592 .ldt_ctx_tags = LCT_MD_THREAD
595 static struct obd_ops osp_obd_device_ops = {
596 .o_owner = THIS_MODULE,
597 .o_add_conn = client_import_add_conn,
598 .o_del_conn = client_import_del_conn,
599 .o_reconnect = osp_reconnect,
600 .o_connect = osp_obd_connect,
601 .o_disconnect = osp_obd_disconnect,
604 static int __init osp_mod_init(void)
606 struct lprocfs_static_vars lvars;
607 cfs_proc_dir_entry_t *osc_proc_dir;
610 rc = lu_kmem_init(osp_caches);
614 lprocfs_osp_init_vars(&lvars);
616 rc = class_register_type(&osp_obd_device_ops, NULL, lvars.module_vars,
617 LUSTRE_OSP_NAME, &osp_device_type);
619 /* create "osc" entry in procfs for compatibility purposes */
621 lu_kmem_fini(osp_caches);
625 osc_proc_dir = lprocfs_srch(proc_lustre_root, "osc");
626 if (osc_proc_dir == NULL) {
627 osc_proc_dir = lprocfs_register("osc", proc_lustre_root, NULL,
629 if (IS_ERR(osc_proc_dir))
630 CERROR("osp: can't create compat entry \"osc\": %d\n",
631 (int) PTR_ERR(osc_proc_dir));
636 static void __exit osp_mod_exit(void)
638 lprocfs_try_remove_proc_entry("osc", proc_lustre_root);
640 class_unregister_type(LUSTRE_OSP_NAME);
641 lu_kmem_fini(osp_caches);
644 MODULE_AUTHOR("Intel, Inc. <http://www.intel.com/>");
645 MODULE_DESCRIPTION("Lustre OST Proxy Device ("LUSTRE_OSP_NAME")");
646 MODULE_LICENSE("GPL");
648 cfs_module(osp, LUSTRE_VERSION_STRING, osp_mod_init, osp_mod_exit);