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);
145 static struct lu_device *lov_device_fini(const struct lu_env *env,
149 struct lov_device *ld = lu2lov_dev(d);
151 LASSERT(ld->ld_lov != NULL);
152 if (ld->ld_target == NULL)
155 lov_foreach_target(ld, i) {
156 struct lovsub_device *lsd;
158 lsd = ld->ld_target[i];
160 cl_stack_fini(env, lovsub2cl_dev(lsd));
161 ld->ld_target[i] = NULL;
167 static int lov_device_init(const struct lu_env *env, struct lu_device *d,
168 const char *name, struct lu_device *next)
170 struct lov_device *ld = lu2lov_dev(d);
174 LASSERT(d->ld_site != NULL);
175 if (ld->ld_target == NULL)
178 lov_foreach_target(ld, i) {
179 struct lovsub_device *lsd;
180 struct cl_device *cl;
181 struct lov_tgt_desc *desc;
183 desc = ld->ld_lov->lov_tgts[i];
187 cl = cl_type_setup(env, d->ld_site, &lovsub_device_type,
188 desc->ltd_obd->obd_lu_dev);
193 lsd = cl2lovsub_dev(cl);
194 ld->ld_target[i] = lsd;
198 lov_device_fini(env, d);
200 ld->ld_flags |= LOV_DEV_INITIALIZED;
205 /* Free the lov specific data created for the back end lu_device. */
206 static struct lu_device *lov_device_free(const struct lu_env *env,
209 struct lov_device *ld = lu2lov_dev(d);
210 const int nr = ld->ld_target_nr;
212 cl_device_fini(lu2cl_dev(d));
213 if (ld->ld_target != NULL)
214 OBD_FREE(ld->ld_target, nr * sizeof ld->ld_target[0]);
220 static void lov_cl_del_target(const struct lu_env *env, struct lu_device *dev,
223 struct lov_device *ld = lu2lov_dev(dev);
226 if (ld->ld_target[index] != NULL) {
227 cl_stack_fini(env, lovsub2cl_dev(ld->ld_target[index]));
228 ld->ld_target[index] = NULL;
233 static int lov_expand_targets(const struct lu_env *env, struct lov_device *dev)
241 tgt_size = dev->ld_lov->lov_tgt_size;
242 sub_size = dev->ld_target_nr;
243 if (sub_size < tgt_size) {
244 struct lovsub_device **newd;
245 const size_t sz = sizeof(newd[0]);
247 OBD_ALLOC(newd, tgt_size * sz);
250 memcpy(newd, dev->ld_target, sub_size * sz);
251 OBD_FREE(dev->ld_target, sub_size * sz);
254 dev->ld_target = newd;
255 dev->ld_target_nr = tgt_size;
264 static int lov_cl_add_target(const struct lu_env *env, struct lu_device *dev,
267 struct obd_device *obd = dev->ld_obd;
268 struct lov_device *ld = lu2lov_dev(dev);
269 struct lov_tgt_desc *tgt;
270 struct lovsub_device *lsd;
271 struct cl_device *cl;
277 tgt = obd->u.lov.lov_tgts[index];
278 LASSERT(tgt != NULL);
279 LASSERT(tgt->ltd_obd != NULL);
281 if (!tgt->ltd_obd->obd_set_up) {
282 CERROR("Target %s not set up\n", obd_uuid2str(&tgt->ltd_uuid));
286 rc = lov_expand_targets(env, ld);
287 if (rc == 0 && ld->ld_flags & LOV_DEV_INITIALIZED) {
288 LASSERT(dev->ld_site != NULL);
290 cl = cl_type_setup(env, dev->ld_site, &lovsub_device_type,
291 tgt->ltd_obd->obd_lu_dev);
293 lsd = cl2lovsub_dev(cl);
294 ld->ld_target[index] = lsd;
296 CERROR("add failed (%d), deleting %s\n", rc,
297 obd_uuid2str(&tgt->ltd_uuid));
298 lov_cl_del_target(env, dev, index);
306 static int lov_process_config(const struct lu_env *env,
307 struct lu_device *d, struct lustre_cfg *cfg)
309 struct obd_device *obd = d->ld_obd;
317 cmd = cfg->lcfg_command;
318 rc = lov_process_config_base(d->ld_obd, cfg, &index, &gen);
321 case LCFG_LOV_ADD_OBD:
322 case LCFG_LOV_ADD_INA:
323 rc = lov_cl_add_target(env, d, index);
325 lov_del_target(d->ld_obd, index, NULL, 0);
327 case LCFG_LOV_DEL_OBD:
328 lov_cl_del_target(env, d, index);
336 static const struct lu_device_operations lov_lu_ops = {
337 .ldo_object_alloc = lov_object_alloc,
338 .ldo_process_config = lov_process_config,
341 static struct lu_device *lov_device_alloc(const struct lu_env *env,
342 struct lu_device_type *t,
343 struct lustre_cfg *cfg)
346 struct lov_device *ld;
347 struct obd_device *obd;
352 RETURN(ERR_PTR(-ENOMEM));
354 cl_device_init(&ld->ld_cl, t);
356 d->ld_ops = &lov_lu_ops;
358 /* setup the LOV OBD */
359 obd = class_name2obd(lustre_cfg_string(cfg, 0));
360 LASSERT(obd != NULL);
361 rc = lov_setup(obd, cfg);
363 lov_device_free(env, d);
367 ld->ld_lov = &obd->u.lov;
371 static const struct lu_device_type_operations lov_device_type_ops = {
372 .ldto_init = lov_type_init,
373 .ldto_fini = lov_type_fini,
375 .ldto_start = lov_type_start,
376 .ldto_stop = lov_type_stop,
378 .ldto_device_alloc = lov_device_alloc,
379 .ldto_device_free = lov_device_free,
381 .ldto_device_init = lov_device_init,
382 .ldto_device_fini = lov_device_fini
385 struct lu_device_type lov_device_type = {
386 .ldt_tags = LU_DEVICE_CL,
387 .ldt_name = LUSTRE_LOV_NAME,
388 .ldt_ops = &lov_device_type_ops,
389 .ldt_ctx_tags = LCT_CL_THREAD