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) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * Implementation of cl_device and cl_device_type for LOV layer.
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
41 #define DEBUG_SUBSYSTEM S_LOV
43 /* class_name2obd() */
44 #include <obd_class.h>
46 #include "lov_cl_internal.h"
48 struct kmem_cache *lov_lock_kmem;
49 struct kmem_cache *lov_object_kmem;
50 struct kmem_cache *lov_thread_kmem;
51 struct kmem_cache *lov_session_kmem;
53 struct kmem_cache *lovsub_lock_kmem;
54 struct kmem_cache *lovsub_object_kmem;
56 struct lu_kmem_descr lov_caches[] = {
58 .ckd_cache = &lov_lock_kmem,
59 .ckd_name = "lov_lock_kmem",
60 .ckd_size = sizeof (struct lov_lock)
63 .ckd_cache = &lov_object_kmem,
64 .ckd_name = "lov_object_kmem",
65 .ckd_size = sizeof (struct lov_object)
68 .ckd_cache = &lov_thread_kmem,
69 .ckd_name = "lov_thread_kmem",
70 .ckd_size = sizeof (struct lov_thread_info)
73 .ckd_cache = &lov_session_kmem,
74 .ckd_name = "lov_session_kmem",
75 .ckd_size = sizeof (struct lov_session)
78 .ckd_cache = &lovsub_lock_kmem,
79 .ckd_name = "lovsub_lock_kmem",
80 .ckd_size = sizeof (struct lovsub_lock)
83 .ckd_cache = &lovsub_object_kmem,
84 .ckd_name = "lovsub_object_kmem",
85 .ckd_size = sizeof (struct lovsub_object)
92 /*****************************************************************************
94 * Lov device and device type functions.
98 static void *lov_key_init(const struct lu_context *ctx,
99 struct lu_context_key *key)
101 struct lov_thread_info *info;
103 OBD_SLAB_ALLOC_PTR_GFP(info, lov_thread_kmem, GFP_NOFS);
105 info = ERR_PTR(-ENOMEM);
109 static void lov_key_fini(const struct lu_context *ctx,
110 struct lu_context_key *key, void *data)
112 struct lov_thread_info *info = data;
113 OBD_SLAB_FREE_PTR(info, lov_thread_kmem);
116 struct lu_context_key lov_key = {
117 .lct_tags = LCT_CL_THREAD,
118 .lct_init = lov_key_init,
119 .lct_fini = lov_key_fini
122 static void *lov_session_key_init(const struct lu_context *ctx,
123 struct lu_context_key *key)
125 struct lov_session *info;
127 OBD_SLAB_ALLOC_PTR_GFP(info, lov_session_kmem, GFP_NOFS);
129 info = ERR_PTR(-ENOMEM);
133 static void lov_session_key_fini(const struct lu_context *ctx,
134 struct lu_context_key *key, void *data)
136 struct lov_session *info = data;
137 OBD_SLAB_FREE_PTR(info, lov_session_kmem);
140 struct lu_context_key lov_session_key = {
141 .lct_tags = LCT_SESSION,
142 .lct_init = lov_session_key_init,
143 .lct_fini = lov_session_key_fini
146 /* type constructor/destructor: lov_type_{init,fini,start,stop}() */
147 LU_TYPE_INIT_FINI(lov, &lov_key, &lov_session_key);
149 static struct lu_device *lov_device_fini(const struct lu_env *env,
153 struct lov_device *ld = lu2lov_dev(d);
155 LASSERT(ld->ld_lov != NULL);
156 if (ld->ld_target == NULL)
159 lov_foreach_target(ld, i) {
160 struct lovsub_device *lsd;
162 lsd = ld->ld_target[i];
164 cl_stack_fini(env, lovsub2cl_dev(lsd));
165 ld->ld_target[i] = NULL;
171 static int lov_device_init(const struct lu_env *env, struct lu_device *d,
172 const char *name, struct lu_device *next)
174 struct lov_device *ld = lu2lov_dev(d);
178 LASSERT(d->ld_site != NULL);
179 if (ld->ld_target == NULL)
182 lov_foreach_target(ld, i) {
183 struct lovsub_device *lsd;
184 struct cl_device *cl;
185 struct lov_tgt_desc *desc;
187 desc = ld->ld_lov->lov_tgts[i];
191 cl = cl_type_setup(env, d->ld_site, &lovsub_device_type,
192 desc->ltd_obd->obd_lu_dev);
197 lsd = cl2lovsub_dev(cl);
198 ld->ld_target[i] = lsd;
202 lov_device_fini(env, d);
204 ld->ld_flags |= LOV_DEV_INITIALIZED;
209 /* Free the lov specific data created for the back end lu_device. */
210 static struct lu_device *lov_device_free(const struct lu_env *env,
213 struct lov_device *ld = lu2lov_dev(d);
214 const int nr = ld->ld_target_nr;
216 cl_device_fini(lu2cl_dev(d));
217 if (ld->ld_target != NULL)
218 OBD_FREE(ld->ld_target, nr * sizeof ld->ld_target[0]);
224 static void lov_cl_del_target(const struct lu_env *env, struct lu_device *dev,
227 struct lov_device *ld = lu2lov_dev(dev);
230 if (ld->ld_target[index] != NULL) {
231 cl_stack_fini(env, lovsub2cl_dev(ld->ld_target[index]));
232 ld->ld_target[index] = NULL;
237 static int lov_expand_targets(const struct lu_env *env, struct lov_device *dev)
245 tgt_size = dev->ld_lov->lov_tgt_size;
246 sub_size = dev->ld_target_nr;
247 if (sub_size < tgt_size) {
248 struct lovsub_device **newd;
249 const size_t sz = sizeof(newd[0]);
251 OBD_ALLOC(newd, tgt_size * sz);
254 memcpy(newd, dev->ld_target, sub_size * sz);
255 OBD_FREE(dev->ld_target, sub_size * sz);
258 dev->ld_target = newd;
259 dev->ld_target_nr = tgt_size;
268 static int lov_cl_add_target(const struct lu_env *env, struct lu_device *dev,
271 struct obd_device *obd = dev->ld_obd;
272 struct lov_device *ld = lu2lov_dev(dev);
273 struct lov_tgt_desc *tgt;
274 struct lovsub_device *lsd;
275 struct cl_device *cl;
281 tgt = obd->u.lov.lov_tgts[index];
282 LASSERT(tgt != NULL);
283 LASSERT(tgt->ltd_obd != NULL);
285 if (!tgt->ltd_obd->obd_set_up) {
286 CERROR("Target %s not set up\n", obd_uuid2str(&tgt->ltd_uuid));
290 rc = lov_expand_targets(env, ld);
291 if (rc == 0 && ld->ld_flags & LOV_DEV_INITIALIZED) {
292 LASSERT(dev->ld_site != NULL);
294 cl = cl_type_setup(env, dev->ld_site, &lovsub_device_type,
295 tgt->ltd_obd->obd_lu_dev);
297 lsd = cl2lovsub_dev(cl);
298 ld->ld_target[index] = lsd;
300 CERROR("add failed (%d), deleting %s\n", rc,
301 obd_uuid2str(&tgt->ltd_uuid));
302 lov_cl_del_target(env, dev, index);
310 static int lov_process_config(const struct lu_env *env,
311 struct lu_device *d, struct lustre_cfg *cfg)
313 struct obd_device *obd = d->ld_obd;
321 cmd = cfg->lcfg_command;
322 rc = lov_process_config_base(d->ld_obd, cfg, &index, &gen);
325 case LCFG_LOV_ADD_OBD:
326 case LCFG_LOV_ADD_INA:
327 rc = lov_cl_add_target(env, d, index);
329 lov_del_target(d->ld_obd, index, NULL, 0);
331 case LCFG_LOV_DEL_OBD:
332 lov_cl_del_target(env, d, index);
340 static const struct lu_device_operations lov_lu_ops = {
341 .ldo_object_alloc = lov_object_alloc,
342 .ldo_process_config = lov_process_config,
345 static struct lu_device *lov_device_alloc(const struct lu_env *env,
346 struct lu_device_type *t,
347 struct lustre_cfg *cfg)
350 struct lov_device *ld;
351 struct obd_device *obd;
356 RETURN(ERR_PTR(-ENOMEM));
358 cl_device_init(&ld->ld_cl, t);
360 d->ld_ops = &lov_lu_ops;
362 /* setup the LOV OBD */
363 obd = class_name2obd(lustre_cfg_string(cfg, 0));
364 LASSERT(obd != NULL);
365 rc = lov_setup(obd, cfg);
367 lov_device_free(env, d);
371 ld->ld_lov = &obd->u.lov;
375 static const struct lu_device_type_operations lov_device_type_ops = {
376 .ldto_init = lov_type_init,
377 .ldto_fini = lov_type_fini,
379 .ldto_start = lov_type_start,
380 .ldto_stop = lov_type_stop,
382 .ldto_device_alloc = lov_device_alloc,
383 .ldto_device_free = lov_device_free,
385 .ldto_device_init = lov_device_init,
386 .ldto_device_fini = lov_device_fini
389 struct lu_device_type lov_device_type = {
390 .ldt_tags = LU_DEVICE_CL,
391 .ldt_name = LUSTRE_LOV_NAME,
392 .ldt_ops = &lov_device_type_ops,
393 .ldt_ctx_tags = LCT_CL_THREAD