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, 2016, 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_lock_kmem;
50 struct kmem_cache *lovsub_object_kmem;
52 struct lu_kmem_descr lov_caches[] = {
54 .ckd_cache = &lov_lock_kmem,
55 .ckd_name = "lov_lock_kmem",
56 .ckd_size = sizeof (struct lov_lock)
59 .ckd_cache = &lov_object_kmem,
60 .ckd_name = "lov_object_kmem",
61 .ckd_size = sizeof (struct lov_object)
64 .ckd_cache = &lov_thread_kmem,
65 .ckd_name = "lov_thread_kmem",
66 .ckd_size = sizeof (struct lov_thread_info)
69 .ckd_cache = &lov_session_kmem,
70 .ckd_name = "lov_session_kmem",
71 .ckd_size = sizeof (struct lov_session)
74 .ckd_cache = &lovsub_lock_kmem,
75 .ckd_name = "lovsub_lock_kmem",
76 .ckd_size = sizeof (struct lovsub_lock)
79 .ckd_cache = &lovsub_object_kmem,
80 .ckd_name = "lovsub_object_kmem",
81 .ckd_size = sizeof (struct lovsub_object)
88 /*****************************************************************************
90 * Lov device and device type functions.
94 static void *lov_key_init(const struct lu_context *ctx,
95 struct lu_context_key *key)
97 struct lov_thread_info *info;
99 OBD_SLAB_ALLOC_PTR_GFP(info, lov_thread_kmem, GFP_NOFS);
101 info = ERR_PTR(-ENOMEM);
105 static void lov_key_fini(const struct lu_context *ctx,
106 struct lu_context_key *key, void *data)
108 struct lov_thread_info *info = data;
109 OBD_SLAB_FREE_PTR(info, lov_thread_kmem);
112 struct lu_context_key lov_key = {
113 .lct_tags = LCT_CL_THREAD,
114 .lct_init = lov_key_init,
115 .lct_fini = lov_key_fini
118 static void *lov_session_key_init(const struct lu_context *ctx,
119 struct lu_context_key *key)
121 struct lov_session *info;
123 OBD_SLAB_ALLOC_PTR_GFP(info, lov_session_kmem, GFP_NOFS);
125 info = ERR_PTR(-ENOMEM);
129 static void lov_session_key_fini(const struct lu_context *ctx,
130 struct lu_context_key *key, void *data)
132 struct lov_session *info = data;
133 OBD_SLAB_FREE_PTR(info, lov_session_kmem);
136 struct lu_context_key lov_session_key = {
137 .lct_tags = LCT_SESSION,
138 .lct_init = lov_session_key_init,
139 .lct_fini = lov_session_key_fini
142 /* type constructor/destructor: lov_type_{init,fini,start,stop}() */
143 LU_TYPE_INIT_FINI(lov, &lov_key, &lov_session_key);
146 static int lov_mdc_dev_init(const struct lu_env *env, struct lov_device *ld,
147 struct lu_device *mdc_dev, __u32 idx, __u32 nr)
149 struct cl_device *cl;
152 cl = cl_type_setup(env, &ld->ld_site, &lovsub_device_type,
157 ld->ld_md_tgts[nr].ldm_mdc = cl;
158 ld->ld_md_tgts[nr].ldm_idx = idx;
162 static struct lu_device *lov_device_fini(const struct lu_env *env,
165 struct lov_device *ld = lu2lov_dev(d);
168 LASSERT(ld->ld_lov != NULL);
170 if (ld->ld_lmv != NULL) {
171 class_decref(ld->ld_lmv, "lov", d);
175 if (ld->ld_md_tgts != NULL) {
176 for (i = 0; i < ld->ld_md_tgts_nr; i++) {
177 if (ld->ld_md_tgts[i].ldm_mdc == NULL)
180 cl_stack_fini(env, ld->ld_md_tgts[i].ldm_mdc);
181 ld->ld_md_tgts[i].ldm_mdc = NULL;
182 ld->ld_lov->lov_mdc_tgts[i].lmtd_mdc = NULL;
186 if (ld->ld_target != NULL) {
187 lov_foreach_target(ld, i) {
188 struct lovsub_device *lsd;
190 lsd = ld->ld_target[i];
192 cl_stack_fini(env, lovsub2cl_dev(lsd));
193 ld->ld_target[i] = NULL;
200 static int lov_device_init(const struct lu_env *env, struct lu_device *d,
201 const char *name, struct lu_device *next)
203 struct lov_device *ld = lu2lov_dev(d);
207 /* check all added already MDC subdevices and initialize them */
208 for (i = 0; i < ld->ld_md_tgts_nr; i++) {
209 struct obd_device *mdc;
212 mdc = ld->ld_lov->lov_mdc_tgts[i].lmtd_mdc;
213 idx = ld->ld_lov->lov_mdc_tgts[i].lmtd_index;
218 rc = lov_mdc_dev_init(env, ld, mdc->obd_lu_dev, idx, i);
220 CERROR("%s: failed to add MDC %s as target: rc = %d\n",
222 obd_uuid2str(&mdc->obd_uuid), rc);
227 if (ld->ld_target == NULL)
230 lov_foreach_target(ld, i) {
231 struct lovsub_device *lsd;
232 struct cl_device *cl;
233 struct lov_tgt_desc *desc;
235 desc = ld->ld_lov->lov_tgts[i];
239 cl = cl_type_setup(env, &ld->ld_site, &lovsub_device_type,
240 desc->ltd_obd->obd_lu_dev);
242 GOTO(out_err, rc = PTR_ERR(cl));
244 lsd = cl2lovsub_dev(cl);
245 ld->ld_target[i] = lsd;
247 ld->ld_flags |= LOV_DEV_INITIALIZED;
255 /* Free the lov specific data created for the back end lu_device. */
256 static struct lu_device *lov_device_free(const struct lu_env *env,
259 struct lov_device *ld = lu2lov_dev(d);
260 const int nr = ld->ld_target_nr;
262 lu_site_fini(&ld->ld_site);
264 cl_device_fini(lu2cl_dev(d));
266 OBD_FREE(ld->ld_target, nr * sizeof ld->ld_target[0]);
267 ld->ld_target = NULL;
269 if (ld->ld_md_tgts) {
270 OBD_FREE(ld->ld_md_tgts,
271 sizeof(*ld->ld_md_tgts) * LOV_MDC_TGT_MAX);
272 ld->ld_md_tgts = NULL;
274 /* free array of MDCs */
275 if (ld->ld_lov->lov_mdc_tgts) {
276 OBD_FREE(ld->ld_lov->lov_mdc_tgts,
277 sizeof(*ld->ld_lov->lov_mdc_tgts) * LOV_MDC_TGT_MAX);
278 ld->ld_lov->lov_mdc_tgts = NULL;
285 static void lov_cl_del_target(const struct lu_env *env, struct lu_device *dev,
288 struct lov_device *ld = lu2lov_dev(dev);
291 if (ld->ld_target[index] != NULL) {
292 cl_stack_fini(env, lovsub2cl_dev(ld->ld_target[index]));
293 ld->ld_target[index] = NULL;
298 static int lov_expand_targets(const struct lu_env *env, struct lov_device *dev)
306 tgt_size = dev->ld_lov->lov_tgt_size;
307 sub_size = dev->ld_target_nr;
308 if (sub_size < tgt_size) {
309 struct lovsub_device **newd;
310 const size_t sz = sizeof(newd[0]);
312 OBD_ALLOC(newd, tgt_size * sz);
315 memcpy(newd, dev->ld_target, sub_size * sz);
316 OBD_FREE(dev->ld_target, sub_size * sz);
319 dev->ld_target = newd;
320 dev->ld_target_nr = tgt_size;
329 static int lov_cl_add_target(const struct lu_env *env, struct lu_device *dev,
332 struct obd_device *obd = dev->ld_obd;
333 struct lov_device *ld = lu2lov_dev(dev);
334 struct lov_tgt_desc *tgt;
335 struct lovsub_device *lsd;
336 struct cl_device *cl;
342 tgt = obd->u.lov.lov_tgts[index];
343 LASSERT(tgt != NULL);
344 LASSERT(tgt->ltd_obd != NULL);
346 if (!tgt->ltd_obd->obd_set_up) {
347 CERROR("Target %s not set up\n", obd_uuid2str(&tgt->ltd_uuid));
351 rc = lov_expand_targets(env, ld);
352 if (rc == 0 && ld->ld_flags & LOV_DEV_INITIALIZED) {
353 cl = cl_type_setup(env, &ld->ld_site, &lovsub_device_type,
354 tgt->ltd_obd->obd_lu_dev);
356 lsd = cl2lovsub_dev(cl);
357 ld->ld_target[index] = lsd;
359 CERROR("add failed (%d), deleting %s\n", rc,
360 obd_uuid2str(&tgt->ltd_uuid));
361 lov_cl_del_target(env, dev, index);
370 * Add new MDC target device in LOV.
372 * This function is part of the configuration log processing. It adds new MDC
373 * device to the MDC device array indexed by their indexes.
375 * \param[in] env execution environment
376 * \param[in] d LU device of LOV device
377 * \param[in] mdc MDC device to add
378 * \param[in] idx MDC device index
380 * \retval 0 if successful
381 * \retval negative value on error
383 static int lov_add_mdc_target(const struct lu_env *env, struct lu_device *d,
384 struct obd_device *mdc, __u32 idx)
386 struct lov_device *ld = lu2lov_dev(d);
387 struct obd_device *lov_obd = d->ld_obd;
388 struct obd_device *lmv_obd;
394 LASSERT(mdc != NULL);
395 if (ld->ld_md_tgts_nr == LOV_MDC_TGT_MAX) {
396 /* If the maximum value of LOV_MDC_TGT_MAX will become too
397 * small then all MD target handling must be rewritten in LOD
398 * manner, check lod_add_device() and related functionality.
400 CERROR("%s: cannot serve more than %d MDC devices\n",
401 lov_obd->obd_name, LOV_MDC_TGT_MAX);
405 /* grab FLD from lmv, do that here, when first MDC is added
406 * to be sure LMV is set up and can be found */
407 if (ld->ld_lmv == NULL) {
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))
416 if (lmv_obd == NULL) {
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;
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;
481 /* modify_mdc_tgts add 0:lustre-clilmv 1:lustre-MDT0000_UUID
482 * 2:0 3:1 4:lustre-MDT0000-mdc_UUID */
483 if (LUSTRE_CFG_BUFLEN(cfg, 1) > sizeof(tgt_uuid.uuid))
484 GOTO(out, rc = -EINVAL);
486 obd_str2uuid(&tgt_uuid, lustre_cfg_buf(cfg, 1));
488 if (sscanf(lustre_cfg_buf(cfg, 2), "%d", &index) != 1)
489 GOTO(out, rc = -EINVAL);
491 mdc = class_find_client_obd(&tgt_uuid, LUSTRE_MDC_NAME,
494 GOTO(out, rc = -ENODEV);
495 rc = lov_add_mdc_target(env, d, mdc, index);
504 static const struct lu_device_operations lov_lu_ops = {
505 .ldo_object_alloc = lov_object_alloc,
506 .ldo_process_config = lov_process_config,
509 static struct lu_device *lov_device_alloc(const struct lu_env *env,
510 struct lu_device_type *t,
511 struct lustre_cfg *cfg)
514 struct lov_device *ld;
515 struct obd_device *obd;
520 RETURN(ERR_PTR(-ENOMEM));
522 cl_device_init(&ld->ld_cl, t);
524 d->ld_ops = &lov_lu_ops;
526 /* setup the LOV OBD */
527 obd = class_name2obd(lustre_cfg_string(cfg, 0));
528 LASSERT(obd != NULL);
529 rc = lov_setup(obd, cfg);
533 /* Alloc MDC devices array */
534 /* XXX: need dynamic allocation at some moment */
535 OBD_ALLOC(ld->ld_md_tgts, sizeof(*ld->ld_md_tgts) * LOV_MDC_TGT_MAX);
536 if (ld->ld_md_tgts == NULL)
537 GOTO(out, rc = -ENOMEM);
539 ld->ld_md_tgts_nr = 0;
541 ld->ld_lov = &obd->u.lov;
542 OBD_ALLOC(ld->ld_lov->lov_mdc_tgts,
543 sizeof(*ld->ld_lov->lov_mdc_tgts) * LOV_MDC_TGT_MAX);
544 if (ld->ld_lov->lov_mdc_tgts == NULL)
545 GOTO(out_md_tgts, rc = -ENOMEM);
547 rc = lu_site_init(&ld->ld_site, d);
549 GOTO(out_mdc_tgts, rc);
551 rc = lu_site_init_finish(&ld->ld_site);
557 lu_site_fini(&ld->ld_site);
559 OBD_FREE(ld->ld_lov->lov_mdc_tgts,
560 sizeof(*ld->ld_lov->lov_mdc_tgts) * LOV_MDC_TGT_MAX);
561 ld->ld_lov->lov_mdc_tgts = NULL;
563 OBD_FREE(ld->ld_md_tgts, sizeof(*ld->ld_md_tgts) * LOV_MDC_TGT_MAX);
564 ld->ld_md_tgts = NULL;
571 static const struct lu_device_type_operations lov_device_type_ops = {
572 .ldto_init = lov_type_init,
573 .ldto_fini = lov_type_fini,
575 .ldto_start = lov_type_start,
576 .ldto_stop = lov_type_stop,
578 .ldto_device_alloc = lov_device_alloc,
579 .ldto_device_free = lov_device_free,
581 .ldto_device_init = lov_device_init,
582 .ldto_device_fini = lov_device_fini
585 struct lu_device_type lov_device_type = {
586 .ldt_tags = LU_DEVICE_CL,
587 .ldt_name = LUSTRE_LOV_NAME,
588 .ldt_ops = &lov_device_type_ops,
589 .ldt_ctx_tags = LCT_CL_THREAD