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 kmem_cache *lov_lock_link_kmem;
58 /** Lock class of lov_device::ld_mutex. */
59 static struct lock_class_key cl_lov_device_mutex_class;
61 struct lu_kmem_descr lov_caches[] = {
63 .ckd_cache = &lov_lock_kmem,
64 .ckd_name = "lov_lock_kmem",
65 .ckd_size = sizeof (struct lov_lock)
68 .ckd_cache = &lov_object_kmem,
69 .ckd_name = "lov_object_kmem",
70 .ckd_size = sizeof (struct lov_object)
73 .ckd_cache = &lov_thread_kmem,
74 .ckd_name = "lov_thread_kmem",
75 .ckd_size = sizeof (struct lov_thread_info)
78 .ckd_cache = &lov_session_kmem,
79 .ckd_name = "lov_session_kmem",
80 .ckd_size = sizeof (struct lov_session)
83 .ckd_cache = &lovsub_lock_kmem,
84 .ckd_name = "lovsub_lock_kmem",
85 .ckd_size = sizeof (struct lovsub_lock)
88 .ckd_cache = &lovsub_object_kmem,
89 .ckd_name = "lovsub_object_kmem",
90 .ckd_size = sizeof (struct lovsub_object)
93 .ckd_cache = &lov_lock_link_kmem,
94 .ckd_name = "lov_lock_link_kmem",
95 .ckd_size = sizeof (struct lov_lock_link)
102 /*****************************************************************************
104 * Lov device and device type functions.
108 static void *lov_key_init(const struct lu_context *ctx,
109 struct lu_context_key *key)
111 struct lov_thread_info *info;
113 OBD_SLAB_ALLOC_PTR_GFP(info, lov_thread_kmem, GFP_NOFS);
115 info = ERR_PTR(-ENOMEM);
119 static void lov_key_fini(const struct lu_context *ctx,
120 struct lu_context_key *key, void *data)
122 struct lov_thread_info *info = data;
123 OBD_SLAB_FREE_PTR(info, lov_thread_kmem);
126 struct lu_context_key lov_key = {
127 .lct_tags = LCT_CL_THREAD,
128 .lct_init = lov_key_init,
129 .lct_fini = lov_key_fini
132 static void *lov_session_key_init(const struct lu_context *ctx,
133 struct lu_context_key *key)
135 struct lov_session *info;
137 OBD_SLAB_ALLOC_PTR_GFP(info, lov_session_kmem, GFP_NOFS);
139 info = ERR_PTR(-ENOMEM);
143 static void lov_session_key_fini(const struct lu_context *ctx,
144 struct lu_context_key *key, void *data)
146 struct lov_session *info = data;
147 OBD_SLAB_FREE_PTR(info, lov_session_kmem);
150 struct lu_context_key lov_session_key = {
151 .lct_tags = LCT_SESSION,
152 .lct_init = lov_session_key_init,
153 .lct_fini = lov_session_key_fini
156 /* type constructor/destructor: lov_type_{init,fini,start,stop}() */
157 LU_TYPE_INIT_FINI(lov, &lov_key, &lov_session_key);
159 static struct lu_device *lov_device_fini(const struct lu_env *env,
163 struct lov_device *ld = lu2lov_dev(d);
165 LASSERT(ld->ld_lov != NULL);
166 if (ld->ld_target == NULL)
169 lov_foreach_target(ld, i) {
170 struct lovsub_device *lsd;
172 lsd = ld->ld_target[i];
174 cl_stack_fini(env, lovsub2cl_dev(lsd));
175 ld->ld_target[i] = NULL;
181 static int lov_device_init(const struct lu_env *env, struct lu_device *d,
182 const char *name, struct lu_device *next)
184 struct lov_device *ld = lu2lov_dev(d);
188 LASSERT(d->ld_site != NULL);
189 if (ld->ld_target == NULL)
192 lov_foreach_target(ld, i) {
193 struct lovsub_device *lsd;
194 struct cl_device *cl;
195 struct lov_tgt_desc *desc;
197 desc = ld->ld_lov->lov_tgts[i];
201 cl = cl_type_setup(env, d->ld_site, &lovsub_device_type,
202 desc->ltd_obd->obd_lu_dev);
207 lsd = cl2lovsub_dev(cl);
209 lsd->acid_super = ld;
210 ld->ld_target[i] = lsd;
214 lov_device_fini(env, d);
216 ld->ld_flags |= LOV_DEV_INITIALIZED;
221 static void lov_emerg_free(struct lov_device_emerg **emrg, int nr)
225 for (i = 0; i < nr; ++i) {
226 struct lov_device_emerg *em;
230 LASSERT(em->emrg_page_list.pl_nr == 0);
231 if (em->emrg_env != NULL)
232 cl_env_put(em->emrg_env, &em->emrg_refcheck);
236 OBD_FREE(emrg, nr * sizeof emrg[0]);
239 static struct lu_device *lov_device_free(const struct lu_env *env,
242 struct lov_device *ld = lu2lov_dev(d);
243 const int nr = ld->ld_target_nr;
245 cl_device_fini(lu2cl_dev(d));
246 if (ld->ld_target != NULL)
247 OBD_FREE(ld->ld_target, nr * sizeof ld->ld_target[0]);
248 if (ld->ld_emrg != NULL)
249 lov_emerg_free(ld->ld_emrg, nr);
254 static void lov_cl_del_target(const struct lu_env *env, struct lu_device *dev,
257 struct lov_device *ld = lu2lov_dev(dev);
260 if (ld->ld_target[index] != NULL) {
261 cl_stack_fini(env, lovsub2cl_dev(ld->ld_target[index]));
262 ld->ld_target[index] = NULL;
267 static struct lov_device_emerg **lov_emerg_alloc(int nr)
269 struct lov_device_emerg **emerg;
273 OBD_ALLOC(emerg, nr * sizeof emerg[0]);
275 return ERR_PTR(-ENOMEM);
276 for (result = i = 0; i < nr && result == 0; i++) {
277 struct lov_device_emerg *em;
282 cl_page_list_init(&em->emrg_page_list);
283 em->emrg_env = cl_env_alloc(&em->emrg_refcheck,
284 LCT_REMEMBER|LCT_NOREF);
285 if (!IS_ERR(em->emrg_env))
286 em->emrg_env->le_ctx.lc_cookie = 0x2;
288 result = PTR_ERR(em->emrg_env);
295 lov_emerg_free(emerg, nr);
296 emerg = ERR_PTR(result);
301 static int lov_expand_targets(const struct lu_env *env, struct lov_device *dev)
309 tgt_size = dev->ld_lov->lov_tgt_size;
310 sub_size = dev->ld_target_nr;
311 if (sub_size < tgt_size) {
312 struct lovsub_device **newd;
313 struct lov_device_emerg **emerg;
314 const size_t sz = sizeof newd[0];
316 emerg = lov_emerg_alloc(tgt_size);
318 RETURN(PTR_ERR(emerg));
320 OBD_ALLOC(newd, tgt_size * sz);
322 mutex_lock(&dev->ld_mutex);
324 memcpy(newd, dev->ld_target, sub_size * sz);
325 OBD_FREE(dev->ld_target, sub_size * sz);
327 dev->ld_target = newd;
328 dev->ld_target_nr = tgt_size;
330 if (dev->ld_emrg != NULL)
331 lov_emerg_free(dev->ld_emrg, sub_size);
332 dev->ld_emrg = emerg;
333 mutex_unlock(&dev->ld_mutex);
335 lov_emerg_free(emerg, tgt_size);
342 static int lov_cl_add_target(const struct lu_env *env, struct lu_device *dev,
345 struct obd_device *obd = dev->ld_obd;
346 struct lov_device *ld = lu2lov_dev(dev);
347 struct lov_tgt_desc *tgt;
348 struct lovsub_device *lsd;
349 struct cl_device *cl;
355 tgt = obd->u.lov.lov_tgts[index];
356 LASSERT(tgt != NULL);
357 LASSERT(tgt->ltd_obd != NULL);
359 if (!tgt->ltd_obd->obd_set_up) {
360 CERROR("Target %s not set up\n", obd_uuid2str(&tgt->ltd_uuid));
364 rc = lov_expand_targets(env, ld);
365 if (rc == 0 && ld->ld_flags & LOV_DEV_INITIALIZED) {
366 LASSERT(dev->ld_site != NULL);
368 cl = cl_type_setup(env, dev->ld_site, &lovsub_device_type,
369 tgt->ltd_obd->obd_lu_dev);
371 lsd = cl2lovsub_dev(cl);
372 lsd->acid_idx = index;
373 lsd->acid_super = ld;
374 ld->ld_target[index] = lsd;
376 CERROR("add failed (%d), deleting %s\n", rc,
377 obd_uuid2str(&tgt->ltd_uuid));
378 lov_cl_del_target(env, dev, index);
386 static int lov_process_config(const struct lu_env *env,
387 struct lu_device *d, struct lustre_cfg *cfg)
389 struct obd_device *obd = d->ld_obd;
397 cmd = cfg->lcfg_command;
398 rc = lov_process_config_base(d->ld_obd, cfg, &index, &gen);
401 case LCFG_LOV_ADD_OBD:
402 case LCFG_LOV_ADD_INA:
403 rc = lov_cl_add_target(env, d, index);
405 lov_del_target(d->ld_obd, index, NULL, 0);
407 case LCFG_LOV_DEL_OBD:
408 lov_cl_del_target(env, d, index);
416 static const struct lu_device_operations lov_lu_ops = {
417 .ldo_object_alloc = lov_object_alloc,
418 .ldo_process_config = lov_process_config,
421 static struct lu_device *lov_device_alloc(const struct lu_env *env,
422 struct lu_device_type *t,
423 struct lustre_cfg *cfg)
426 struct lov_device *ld;
427 struct obd_device *obd;
432 RETURN(ERR_PTR(-ENOMEM));
434 cl_device_init(&ld->ld_cl, t);
436 d->ld_ops = &lov_lu_ops;
438 mutex_init(&ld->ld_mutex);
439 lockdep_set_class(&ld->ld_mutex, &cl_lov_device_mutex_class);
441 /* setup the LOV OBD */
442 obd = class_name2obd(lustre_cfg_string(cfg, 0));
443 LASSERT(obd != NULL);
444 rc = lov_setup(obd, cfg);
446 lov_device_free(env, d);
450 ld->ld_lov = &obd->u.lov;
454 static const struct lu_device_type_operations lov_device_type_ops = {
455 .ldto_init = lov_type_init,
456 .ldto_fini = lov_type_fini,
458 .ldto_start = lov_type_start,
459 .ldto_stop = lov_type_stop,
461 .ldto_device_alloc = lov_device_alloc,
462 .ldto_device_free = lov_device_free,
464 .ldto_device_init = lov_device_init,
465 .ldto_device_fini = lov_device_fini
468 struct lu_device_type lov_device_type = {
469 .ldt_tags = LU_DEVICE_CL,
470 .ldt_name = LUSTRE_LOV_NAME,
471 .ldt_ops = &lov_device_type_ops,
472 .ldt_ctx_tags = LCT_CL_THREAD