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, 2015, 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"
43 #include "lov_internal.h"
45 struct kmem_cache *lov_lock_kmem;
46 struct kmem_cache *lov_object_kmem;
47 struct kmem_cache *lov_thread_kmem;
48 struct kmem_cache *lov_session_kmem;
50 struct kmem_cache *lovsub_lock_kmem;
51 struct kmem_cache *lovsub_object_kmem;
53 struct lu_kmem_descr lov_caches[] = {
55 .ckd_cache = &lov_lock_kmem,
56 .ckd_name = "lov_lock_kmem",
57 .ckd_size = sizeof(struct lov_lock)
60 .ckd_cache = &lov_object_kmem,
61 .ckd_name = "lov_object_kmem",
62 .ckd_size = sizeof(struct lov_object)
65 .ckd_cache = &lov_thread_kmem,
66 .ckd_name = "lov_thread_kmem",
67 .ckd_size = sizeof(struct lov_thread_info)
70 .ckd_cache = &lov_session_kmem,
71 .ckd_name = "lov_session_kmem",
72 .ckd_size = sizeof(struct lov_session)
75 .ckd_cache = &lovsub_lock_kmem,
76 .ckd_name = "lovsub_lock_kmem",
77 .ckd_size = sizeof(struct lovsub_lock)
80 .ckd_cache = &lovsub_object_kmem,
81 .ckd_name = "lovsub_object_kmem",
82 .ckd_size = sizeof(struct lovsub_object)
89 /*****************************************************************************
91 * Lov device and device type functions.
95 static void *lov_key_init(const struct lu_context *ctx,
96 struct lu_context_key *key)
98 struct lov_thread_info *info;
100 info = kmem_cache_zalloc(lov_thread_kmem, GFP_NOFS);
102 info = ERR_PTR(-ENOMEM);
106 static void lov_key_fini(const struct lu_context *ctx,
107 struct lu_context_key *key, void *data)
109 struct lov_thread_info *info = data;
111 kmem_cache_free(lov_thread_kmem, info);
114 struct lu_context_key lov_key = {
115 .lct_tags = LCT_CL_THREAD,
116 .lct_init = lov_key_init,
117 .lct_fini = lov_key_fini
120 static void *lov_session_key_init(const struct lu_context *ctx,
121 struct lu_context_key *key)
123 struct lov_session *info;
125 info = kmem_cache_zalloc(lov_session_kmem, GFP_NOFS);
127 info = ERR_PTR(-ENOMEM);
131 static void lov_session_key_fini(const struct lu_context *ctx,
132 struct lu_context_key *key, void *data)
134 struct lov_session *info = data;
136 kmem_cache_free(lov_session_kmem, info);
139 struct lu_context_key lov_session_key = {
140 .lct_tags = LCT_SESSION,
141 .lct_init = lov_session_key_init,
142 .lct_fini = lov_session_key_fini
145 /* type constructor/destructor: lov_type_{init,fini,start,stop}() */
146 LU_TYPE_INIT_FINI(lov, &lov_key, &lov_session_key);
148 static struct lu_device *lov_device_fini(const struct lu_env *env,
152 struct lov_device *ld = lu2lov_dev(d);
158 lov_foreach_target(ld, i) {
159 struct lovsub_device *lsd;
161 lsd = ld->ld_target[i];
163 cl_stack_fini(env, lovsub2cl_dev(lsd));
164 ld->ld_target[i] = NULL;
170 static int lov_device_init(const struct lu_env *env, struct lu_device *d,
171 const char *name, struct lu_device *next)
173 struct lov_device *ld = lu2lov_dev(d);
181 lov_foreach_target(ld, i) {
182 struct lovsub_device *lsd;
183 struct cl_device *cl;
184 struct lov_tgt_desc *desc;
186 desc = ld->ld_lov->lov_tgts[i];
190 cl = cl_type_setup(env, d->ld_site, &lovsub_device_type,
191 desc->ltd_obd->obd_lu_dev);
196 lsd = cl2lovsub_dev(cl);
197 ld->ld_target[i] = lsd;
201 lov_device_fini(env, d);
203 ld->ld_flags |= LOV_DEV_INITIALIZED;
208 static struct lu_device *lov_device_free(const struct lu_env *env,
211 struct lov_device *ld = lu2lov_dev(d);
213 cl_device_fini(lu2cl_dev(d));
214 kfree(ld->ld_target);
219 static void lov_cl_del_target(const struct lu_env *env, struct lu_device *dev,
222 struct lov_device *ld = lu2lov_dev(dev);
224 if (ld->ld_target[index]) {
225 cl_stack_fini(env, lovsub2cl_dev(ld->ld_target[index]));
226 ld->ld_target[index] = NULL;
230 static int lov_expand_targets(const struct lu_env *env, struct lov_device *dev)
237 tgt_size = dev->ld_lov->lov_tgt_size;
238 sub_size = dev->ld_target_nr;
239 if (sub_size < tgt_size) {
240 struct lovsub_device **newd;
241 const size_t sz = sizeof(newd[0]);
243 newd = kcalloc(tgt_size, sz, GFP_NOFS);
246 memcpy(newd, dev->ld_target, sub_size * sz);
247 kfree(dev->ld_target);
249 dev->ld_target = newd;
250 dev->ld_target_nr = tgt_size;
258 static int lov_cl_add_target(const struct lu_env *env, struct lu_device *dev,
261 struct obd_device *obd = dev->ld_obd;
262 struct lov_device *ld = lu2lov_dev(dev);
263 struct lov_tgt_desc *tgt;
264 struct lovsub_device *lsd;
265 struct cl_device *cl;
270 tgt = obd->u.lov.lov_tgts[index];
272 if (!tgt->ltd_obd->obd_set_up) {
273 CERROR("Target %s not set up\n", obd_uuid2str(&tgt->ltd_uuid));
277 rc = lov_expand_targets(env, ld);
278 if (rc == 0 && ld->ld_flags & LOV_DEV_INITIALIZED) {
279 LASSERT(dev->ld_site);
281 cl = cl_type_setup(env, dev->ld_site, &lovsub_device_type,
282 tgt->ltd_obd->obd_lu_dev);
284 lsd = cl2lovsub_dev(cl);
285 ld->ld_target[index] = lsd;
287 CERROR("add failed (%d), deleting %s\n", rc,
288 obd_uuid2str(&tgt->ltd_uuid));
289 lov_cl_del_target(env, dev, index);
297 static int lov_process_config(const struct lu_env *env,
298 struct lu_device *d, struct lustre_cfg *cfg)
300 struct obd_device *obd = d->ld_obd;
308 cmd = cfg->lcfg_command;
309 rc = lov_process_config_base(d->ld_obd, cfg, &index, &gen);
312 case LCFG_LOV_ADD_OBD:
313 case LCFG_LOV_ADD_INA:
314 rc = lov_cl_add_target(env, d, index);
316 lov_del_target(d->ld_obd, index, NULL, 0);
318 case LCFG_LOV_DEL_OBD:
319 lov_cl_del_target(env, d, index);
327 static const struct lu_device_operations lov_lu_ops = {
328 .ldo_object_alloc = lov_object_alloc,
329 .ldo_process_config = lov_process_config,
332 static struct lu_device *lov_device_alloc(const struct lu_env *env,
333 struct lu_device_type *t,
334 struct lustre_cfg *cfg)
337 struct lov_device *ld;
338 struct obd_device *obd;
341 ld = kzalloc(sizeof(*ld), GFP_NOFS);
343 return ERR_PTR(-ENOMEM);
345 cl_device_init(&ld->ld_cl, t);
347 d->ld_ops = &lov_lu_ops;
349 /* setup the LOV OBD */
350 obd = class_name2obd(lustre_cfg_string(cfg, 0));
352 rc = lov_setup(obd, cfg);
354 lov_device_free(env, d);
358 ld->ld_lov = &obd->u.lov;
362 static const struct lu_device_type_operations lov_device_type_ops = {
363 .ldto_init = lov_type_init,
364 .ldto_fini = lov_type_fini,
366 .ldto_start = lov_type_start,
367 .ldto_stop = lov_type_stop,
369 .ldto_device_alloc = lov_device_alloc,
370 .ldto_device_free = lov_device_free,
372 .ldto_device_init = lov_device_init,
373 .ldto_device_fini = lov_device_fini
376 struct lu_device_type lov_device_type = {
377 .ldt_tags = LU_DEVICE_CL,
378 .ldt_name = LUSTRE_LOV_NAME,
379 .ldt_ops = &lov_device_type_ops,
380 .ldt_ctx_tags = LCT_CL_THREAD