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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * Implementation of cl_device and cl_device_type for LOV layer.
34 * Author: Nikita Danilov <nikita.danilov@sun.com>
37 #define DEBUG_SUBSYSTEM S_LOV
39 /* class_name2obd() */
40 #include <obd_class.h>
42 #include "lov_cl_internal.h"
44 struct kmem_cache *lov_lock_kmem;
45 struct kmem_cache *lov_object_kmem;
46 struct kmem_cache *lov_thread_kmem;
47 struct kmem_cache *lov_session_kmem;
49 struct kmem_cache *lovsub_object_kmem;
51 struct lu_kmem_descr lov_caches[] = {
53 .ckd_cache = &lov_lock_kmem,
54 .ckd_name = "lov_lock_kmem",
55 .ckd_size = sizeof(struct lov_lock)
58 .ckd_cache = &lov_object_kmem,
59 .ckd_name = "lov_object_kmem",
60 .ckd_size = sizeof(struct lov_object)
63 .ckd_cache = &lov_thread_kmem,
64 .ckd_name = "lov_thread_kmem",
65 .ckd_size = sizeof(struct lov_thread_info)
68 .ckd_cache = &lov_session_kmem,
69 .ckd_name = "lov_session_kmem",
70 .ckd_size = sizeof(struct lov_session)
73 .ckd_cache = &lovsub_object_kmem,
74 .ckd_name = "lovsub_object_kmem",
75 .ckd_size = sizeof(struct lovsub_object)
82 /*****************************************************************************
84 * Lov device and device type functions.
88 static void *lov_key_init(const struct lu_context *ctx,
89 struct lu_context_key *key)
91 struct lov_thread_info *info;
93 OBD_SLAB_ALLOC_PTR_GFP(info, lov_thread_kmem, GFP_NOFS);
95 info = ERR_PTR(-ENOMEM);
99 static void lov_key_fini(const struct lu_context *ctx,
100 struct lu_context_key *key, void *data)
102 struct lov_thread_info *info = data;
103 OBD_SLAB_FREE_PTR(info, lov_thread_kmem);
106 struct lu_context_key lov_key = {
107 .lct_tags = LCT_CL_THREAD,
108 .lct_init = lov_key_init,
109 .lct_fini = lov_key_fini
112 static void *lov_session_key_init(const struct lu_context *ctx,
113 struct lu_context_key *key)
115 struct lov_session *info;
117 OBD_SLAB_ALLOC_PTR_GFP(info, lov_session_kmem, GFP_NOFS);
119 info = ERR_PTR(-ENOMEM);
123 static void lov_session_key_fini(const struct lu_context *ctx,
124 struct lu_context_key *key, void *data)
126 struct lov_session *info = data;
128 OBD_SLAB_FREE_PTR(info, lov_session_kmem);
131 struct lu_context_key lov_session_key = {
132 .lct_tags = LCT_SESSION,
133 .lct_init = lov_session_key_init,
134 .lct_fini = lov_session_key_fini
137 /* type constructor/destructor: lov_type_{init,fini,start,stop}() */
138 LU_TYPE_INIT_FINI(lov, &lov_key, &lov_session_key);
141 static int lov_mdc_dev_init(const struct lu_env *env, struct lov_device *ld,
142 struct lu_device *mdc_dev, __u32 idx, __u32 nr)
144 struct cl_device *cl;
147 cl = cl_type_setup(env, &ld->ld_site, &lovsub_device_type,
152 ld->ld_md_tgts[nr].ldm_mdc = cl;
153 ld->ld_md_tgts[nr].ldm_idx = idx;
157 static struct lu_device *lov_device_fini(const struct lu_env *env,
160 struct lov_device *ld = lu2lov_dev(d);
163 LASSERT(ld->ld_lov != NULL);
166 class_decref(ld->ld_lmv, "lov", d);
170 if (ld->ld_md_tgts) {
171 for (i = 0; i < ld->ld_md_tgts_nr; i++) {
172 if (!ld->ld_md_tgts[i].ldm_mdc)
175 cl_stack_fini(env, ld->ld_md_tgts[i].ldm_mdc);
176 ld->ld_md_tgts[i].ldm_mdc = NULL;
177 ld->ld_lov->lov_mdc_tgts[i].lmtd_mdc = NULL;
182 lov_foreach_target(ld, i) {
183 struct lovsub_device *lsd;
185 lsd = ld->ld_target[i];
187 cl_stack_fini(env, lovsub2cl_dev(lsd));
188 ld->ld_target[i] = NULL;
195 static int lov_device_init(const struct lu_env *env, struct lu_device *d,
196 const char *name, struct lu_device *next)
198 struct lov_device *ld = lu2lov_dev(d);
202 /* check all added already MDC subdevices and initialize them */
203 for (i = 0; i < ld->ld_md_tgts_nr; i++) {
204 struct obd_device *mdc;
207 mdc = ld->ld_lov->lov_mdc_tgts[i].lmtd_mdc;
208 idx = ld->ld_lov->lov_mdc_tgts[i].lmtd_index;
213 rc = lov_mdc_dev_init(env, ld, mdc->obd_lu_dev, idx, i);
215 CERROR("%s: failed to add MDC %s as target: rc = %d\n",
217 obd_uuid2str(&mdc->obd_uuid), rc);
225 lov_foreach_target(ld, i) {
226 struct lovsub_device *lsd;
227 struct cl_device *cl;
228 struct lov_tgt_desc *desc;
230 desc = ld->ld_lov->lov_tgts[i];
234 cl = cl_type_setup(env, &ld->ld_site, &lovsub_device_type,
235 desc->ltd_obd->obd_lu_dev);
237 GOTO(out_err, rc = PTR_ERR(cl));
239 lsd = cl2lovsub_dev(cl);
240 ld->ld_target[i] = lsd;
242 ld->ld_flags |= LOV_DEV_INITIALIZED;
250 /* Free the lov specific data created for the back end lu_device. */
251 static struct lu_device *lov_device_free(const struct lu_env *env,
254 struct lov_device *ld = lu2lov_dev(d);
255 const int nr = ld->ld_target_nr;
257 lu_site_fini(&ld->ld_site);
259 cl_device_fini(lu2cl_dev(d));
261 OBD_FREE_PTR_ARRAY(ld->ld_target, nr);
262 ld->ld_target = NULL;
264 if (ld->ld_md_tgts) {
265 OBD_FREE_PTR_ARRAY(ld->ld_md_tgts, LOV_MDC_TGT_MAX);
266 ld->ld_md_tgts = NULL;
268 /* free array of MDCs */
269 if (ld->ld_lov->lov_mdc_tgts) {
270 OBD_FREE_PTR_ARRAY(ld->ld_lov->lov_mdc_tgts, LOV_MDC_TGT_MAX);
271 ld->ld_lov->lov_mdc_tgts = NULL;
278 static void lov_cl_del_target(const struct lu_env *env, struct lu_device *dev,
281 struct lov_device *ld = lu2lov_dev(dev);
285 if (ld->ld_target[index]) {
286 cl_stack_fini(env, lovsub2cl_dev(ld->ld_target[index]));
287 ld->ld_target[index] = NULL;
292 static int lov_expand_targets(const struct lu_env *env, struct lov_device *dev)
300 tgt_size = dev->ld_lov->lov_tgt_size;
301 sub_size = dev->ld_target_nr;
302 if (sub_size < tgt_size) {
303 struct lovsub_device **newd;
304 const size_t sz = sizeof(newd[0]);
306 OBD_ALLOC_PTR_ARRAY(newd, tgt_size);
309 memcpy(newd, dev->ld_target, sub_size * sz);
310 OBD_FREE(dev->ld_target, sub_size * sz);
313 dev->ld_target = newd;
314 dev->ld_target_nr = tgt_size;
323 static int lov_cl_add_target(const struct lu_env *env, struct lu_device *dev,
326 struct obd_device *obd = dev->ld_obd;
327 struct lov_device *ld = lu2lov_dev(dev);
328 struct lov_tgt_desc *tgt;
329 struct lovsub_device *lsd;
330 struct cl_device *cl;
335 lov_tgts_getref(obd);
337 tgt = obd->u.lov.lov_tgts[index];
338 LASSERT(tgt != NULL);
339 LASSERT(tgt->ltd_obd != NULL);
341 if (!tgt->ltd_obd->obd_set_up) {
342 CERROR("Target %s not set up\n", obd_uuid2str(&tgt->ltd_uuid));
346 rc = lov_expand_targets(env, ld);
347 if (rc == 0 && ld->ld_flags & LOV_DEV_INITIALIZED) {
348 cl = cl_type_setup(env, &ld->ld_site, &lovsub_device_type,
349 tgt->ltd_obd->obd_lu_dev);
351 lsd = cl2lovsub_dev(cl);
352 ld->ld_target[index] = lsd;
354 CERROR("add failed (%d), deleting %s\n", rc,
355 obd_uuid2str(&tgt->ltd_uuid));
356 lov_cl_del_target(env, dev, index);
361 lov_tgts_putref(obd);
367 * Add new MDC target device in LOV.
369 * This function is part of the configuration log processing. It adds new MDC
370 * device to the MDC device array indexed by their indexes.
372 * \param[in] env execution environment
373 * \param[in] d LU device of LOV device
374 * \param[in] mdc MDC device to add
375 * \param[in] idx MDC device index
377 * \retval 0 if successful
378 * \retval negative value on error
380 static int lov_add_mdc_target(const struct lu_env *env, struct lu_device *d,
381 struct obd_device *mdc, __u32 idx)
383 struct lov_device *ld = lu2lov_dev(d);
384 struct obd_device *lov_obd = d->ld_obd;
385 struct obd_device *lmv_obd;
391 LASSERT(mdc != NULL);
392 if (ld->ld_md_tgts_nr == LOV_MDC_TGT_MAX) {
394 * If the maximum value of LOV_MDC_TGT_MAX will become too
395 * small then all MD target handling must be rewritten in LOD
396 * manner, check lod_add_device() and related functionality.
398 CERROR("%s: cannot serve more than %d MDC devices\n",
399 lov_obd->obd_name, LOV_MDC_TGT_MAX);
404 * grab FLD from lmv, do that here, when first MDC is added
405 * to be sure LMV is set up and can be found
409 while ((lmv_obd = class_devices_in_group(&lov_obd->obd_uuid,
411 if ((strncmp(lmv_obd->obd_type->typ_name,
413 strlen(LUSTRE_LMV_NAME)) == 0))
417 CERROR("%s: cannot find LMV OBD by UUID (%s)\n",
419 obd_uuid2str(&lmv_obd->obd_uuid));
422 spin_lock(&lmv_obd->obd_dev_lock);
423 class_incref(lmv_obd, "lov", ld);
424 spin_unlock(&lmv_obd->obd_dev_lock);
425 ld->ld_lmv = lmv_obd;
428 LASSERT(lov_obd->u.lov.lov_mdc_tgts[ld->ld_md_tgts_nr].lmtd_mdc ==
431 if (ld->ld_flags & LOV_DEV_INITIALIZED) {
432 rc = lov_mdc_dev_init(env, ld, mdc->obd_lu_dev, idx,
435 CERROR("%s: failed to add MDC %s as target: rc = %d\n",
436 lov_obd->obd_name, obd_uuid2str(&mdc->obd_uuid),
442 lov_obd->u.lov.lov_mdc_tgts[ld->ld_md_tgts_nr].lmtd_mdc = mdc;
443 lov_obd->u.lov.lov_mdc_tgts[ld->ld_md_tgts_nr].lmtd_index = idx;
449 static int lov_process_config(const struct lu_env *env,
450 struct lu_device *d, struct lustre_cfg *cfg)
452 struct obd_device *obd = d->ld_obd;
458 lov_tgts_getref(obd);
460 cmd = cfg->lcfg_command;
462 rc = lov_process_config_base(d->ld_obd, cfg, &index, &gen);
467 case LCFG_LOV_ADD_OBD:
468 case LCFG_LOV_ADD_INA:
469 rc = lov_cl_add_target(env, d, index);
471 lov_del_target(d->ld_obd, index, NULL, 0);
473 case LCFG_LOV_DEL_OBD:
474 lov_cl_del_target(env, d, index);
478 struct obd_device *mdc;
479 struct obd_uuid tgt_uuid;
482 * modify_mdc_tgts add 0:lustre-clilmv 1:lustre-MDT0000_UUID
483 * 2:0 3:1 4:lustre-MDT0000-mdc_UUID
485 if (LUSTRE_CFG_BUFLEN(cfg, 1) > sizeof(tgt_uuid.uuid))
486 GOTO(out, rc = -EINVAL);
488 obd_str2uuid(&tgt_uuid, lustre_cfg_buf(cfg, 1));
490 rc = kstrtou32(lustre_cfg_buf(cfg, 2), 10, &index);
494 mdc = class_find_client_obd(&tgt_uuid, LUSTRE_MDC_NAME,
497 GOTO(out, rc = -ENODEV);
498 rc = lov_add_mdc_target(env, d, mdc, index);
503 lov_tgts_putref(obd);
507 static const struct lu_device_operations lov_lu_ops = {
508 .ldo_object_alloc = lov_object_alloc,
509 .ldo_process_config = lov_process_config,
512 static struct lu_device *lov_device_alloc(const struct lu_env *env,
513 struct lu_device_type *t,
514 struct lustre_cfg *cfg)
517 struct lov_device *ld;
518 struct obd_device *obd;
523 RETURN(ERR_PTR(-ENOMEM));
525 cl_device_init(&ld->ld_cl, t);
527 d->ld_ops = &lov_lu_ops;
529 /* setup the LOV OBD */
530 obd = class_name2obd(lustre_cfg_string(cfg, 0));
531 LASSERT(obd != NULL);
532 rc = lov_setup(obd, cfg);
536 /* Alloc MDC devices array */
537 /* XXX: need dynamic allocation at some moment */
538 OBD_ALLOC_PTR_ARRAY(ld->ld_md_tgts, LOV_MDC_TGT_MAX);
540 GOTO(out, rc = -ENOMEM);
542 ld->ld_md_tgts_nr = 0;
544 ld->ld_lov = &obd->u.lov;
545 OBD_ALLOC_PTR_ARRAY(ld->ld_lov->lov_mdc_tgts, LOV_MDC_TGT_MAX);
546 if (!ld->ld_lov->lov_mdc_tgts)
547 GOTO(out_md_tgts, rc = -ENOMEM);
549 rc = lu_site_init(&ld->ld_site, d);
551 GOTO(out_mdc_tgts, rc);
553 rc = lu_site_init_finish(&ld->ld_site);
559 lu_site_fini(&ld->ld_site);
561 OBD_FREE_PTR_ARRAY(ld->ld_lov->lov_mdc_tgts, LOV_MDC_TGT_MAX);
562 ld->ld_lov->lov_mdc_tgts = NULL;
564 OBD_FREE_PTR_ARRAY(ld->ld_md_tgts, LOV_MDC_TGT_MAX);
565 ld->ld_md_tgts = NULL;
572 static const struct lu_device_type_operations lov_device_type_ops = {
573 .ldto_init = lov_type_init,
574 .ldto_fini = lov_type_fini,
576 .ldto_start = lov_type_start,
577 .ldto_stop = lov_type_stop,
579 .ldto_device_alloc = lov_device_alloc,
580 .ldto_device_free = lov_device_free,
582 .ldto_device_init = lov_device_init,
583 .ldto_device_fini = lov_device_fini
586 struct lu_device_type lov_device_type = {
587 .ldt_tags = LU_DEVICE_CL,
588 .ldt_name = LUSTRE_LOV_NAME,
589 .ldt_ops = &lov_device_type_ops,
590 .ldt_ctx_tags = LCT_CL_THREAD