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, 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 cfs_mem_cache_t *lov_lock_kmem;
49 cfs_mem_cache_t *lov_object_kmem;
50 cfs_mem_cache_t *lov_thread_kmem;
51 cfs_mem_cache_t *lov_session_kmem;
52 cfs_mem_cache_t *lov_req_kmem;
54 cfs_mem_cache_t *lovsub_lock_kmem;
55 cfs_mem_cache_t *lovsub_object_kmem;
56 cfs_mem_cache_t *lovsub_req_kmem;
58 cfs_mem_cache_t *lov_lock_link_kmem;
60 /** Lock class of lov_device::ld_mutex. */
61 struct lock_class_key cl_lov_device_mutex_class;
63 struct lu_kmem_descr lov_caches[] = {
65 .ckd_cache = &lov_lock_kmem,
66 .ckd_name = "lov_lock_kmem",
67 .ckd_size = sizeof (struct lov_lock)
70 .ckd_cache = &lov_object_kmem,
71 .ckd_name = "lov_object_kmem",
72 .ckd_size = sizeof (struct lov_object)
75 .ckd_cache = &lov_thread_kmem,
76 .ckd_name = "lov_thread_kmem",
77 .ckd_size = sizeof (struct lov_thread_info)
80 .ckd_cache = &lov_session_kmem,
81 .ckd_name = "lov_session_kmem",
82 .ckd_size = sizeof (struct lov_session)
85 .ckd_cache = &lov_req_kmem,
86 .ckd_name = "lov_req_kmem",
87 .ckd_size = sizeof (struct lov_req)
90 .ckd_cache = &lovsub_lock_kmem,
91 .ckd_name = "lovsub_lock_kmem",
92 .ckd_size = sizeof (struct lovsub_lock)
95 .ckd_cache = &lovsub_object_kmem,
96 .ckd_name = "lovsub_object_kmem",
97 .ckd_size = sizeof (struct lovsub_object)
100 .ckd_cache = &lovsub_req_kmem,
101 .ckd_name = "lovsub_req_kmem",
102 .ckd_size = sizeof (struct lovsub_req)
105 .ckd_cache = &lov_lock_link_kmem,
106 .ckd_name = "lov_lock_link_kmem",
107 .ckd_size = sizeof (struct lov_lock_link)
114 /*****************************************************************************
116 * Lov transfer operations.
120 static void lov_req_completion(const struct lu_env *env,
121 const struct cl_req_slice *slice, int ioret)
126 lr = cl2lov_req(slice);
127 OBD_SLAB_FREE_PTR(lr, lov_req_kmem);
131 static const struct cl_req_operations lov_req_ops = {
132 .cro_completion = lov_req_completion
135 /*****************************************************************************
137 * Lov device and device type functions.
141 static void *lov_key_init(const struct lu_context *ctx,
142 struct lu_context_key *key)
144 struct lov_thread_info *info;
146 OBD_SLAB_ALLOC_PTR_GFP(info, lov_thread_kmem, CFS_ALLOC_IO);
148 CFS_INIT_LIST_HEAD(&info->lti_closure.clc_list);
150 info = ERR_PTR(-ENOMEM);
154 static void lov_key_fini(const struct lu_context *ctx,
155 struct lu_context_key *key, void *data)
157 struct lov_thread_info *info = data;
158 LINVRNT(cfs_list_empty(&info->lti_closure.clc_list));
159 OBD_SLAB_FREE_PTR(info, lov_thread_kmem);
162 struct lu_context_key lov_key = {
163 .lct_tags = LCT_CL_THREAD,
164 .lct_init = lov_key_init,
165 .lct_fini = lov_key_fini
168 static void *lov_session_key_init(const struct lu_context *ctx,
169 struct lu_context_key *key)
171 struct lov_session *info;
173 OBD_SLAB_ALLOC_PTR_GFP(info, lov_session_kmem, CFS_ALLOC_IO);
175 info = ERR_PTR(-ENOMEM);
179 static void lov_session_key_fini(const struct lu_context *ctx,
180 struct lu_context_key *key, void *data)
182 struct lov_session *info = data;
183 OBD_SLAB_FREE_PTR(info, lov_session_kmem);
186 struct lu_context_key lov_session_key = {
187 .lct_tags = LCT_SESSION,
188 .lct_init = lov_session_key_init,
189 .lct_fini = lov_session_key_fini
192 /* type constructor/destructor: lov_type_{init,fini,start,stop}() */
193 LU_TYPE_INIT_FINI(lov, &lov_key, &lov_session_key);
195 static struct lu_device *lov_device_fini(const struct lu_env *env,
199 struct lov_device *ld = lu2lov_dev(d);
201 LASSERT(ld->ld_lov != NULL);
202 if (ld->ld_target == NULL)
205 lov_foreach_target(ld, i) {
206 struct lovsub_device *lsd;
208 lsd = ld->ld_target[i];
210 cl_stack_fini(env, lovsub2cl_dev(lsd));
211 ld->ld_target[i] = NULL;
217 static int lov_device_init(const struct lu_env *env, struct lu_device *d,
218 const char *name, struct lu_device *next)
220 struct lov_device *ld = lu2lov_dev(d);
224 LASSERT(d->ld_site != NULL);
225 if (ld->ld_target == NULL)
228 lov_foreach_target(ld, i) {
229 struct lovsub_device *lsd;
230 struct cl_device *cl;
231 struct lov_tgt_desc *desc;
233 desc = ld->ld_lov->lov_tgts[i];
237 cl = cl_type_setup(env, d->ld_site, &lovsub_device_type,
238 desc->ltd_obd->obd_lu_dev);
243 lsd = cl2lovsub_dev(cl);
245 lsd->acid_super = ld;
246 ld->ld_target[i] = lsd;
250 lov_device_fini(env, d);
252 ld->ld_flags |= LOV_DEV_INITIALIZED;
257 static int lov_req_init(const struct lu_env *env, struct cl_device *dev,
264 OBD_SLAB_ALLOC_PTR_GFP(lr, lov_req_kmem, CFS_ALLOC_IO);
266 cl_req_slice_add(req, &lr->lr_cl, dev, &lov_req_ops);
273 static const struct cl_device_operations lov_cl_ops = {
274 .cdo_req_init = lov_req_init
277 static void lov_emerg_free(struct lov_device_emerg **emrg, int nr)
281 for (i = 0; i < nr; ++i) {
282 struct lov_device_emerg *em;
286 LASSERT(em->emrg_page_list.pl_nr == 0);
287 if (em->emrg_env != NULL)
288 cl_env_put(em->emrg_env, &em->emrg_refcheck);
292 OBD_FREE(emrg, nr * sizeof emrg[0]);
295 static struct lu_device *lov_device_free(const struct lu_env *env,
298 struct lov_device *ld = lu2lov_dev(d);
299 const int nr = ld->ld_target_nr;
301 cl_device_fini(lu2cl_dev(d));
302 if (ld->ld_target != NULL)
303 OBD_FREE(ld->ld_target, nr * sizeof ld->ld_target[0]);
304 if (ld->ld_emrg != NULL)
305 lov_emerg_free(ld->ld_emrg, nr);
310 static void lov_cl_del_target(const struct lu_env *env, struct lu_device *dev,
313 struct lov_device *ld = lu2lov_dev(dev);
316 if (ld->ld_target[index] != NULL) {
317 cl_stack_fini(env, lovsub2cl_dev(ld->ld_target[index]));
318 ld->ld_target[index] = NULL;
323 static struct lov_device_emerg **lov_emerg_alloc(int nr)
325 struct lov_device_emerg **emerg;
329 OBD_ALLOC(emerg, nr * sizeof emerg[0]);
331 return ERR_PTR(-ENOMEM);
332 for (result = i = 0; i < nr && result == 0; i++) {
333 struct lov_device_emerg *em;
338 cl_page_list_init(&em->emrg_page_list);
339 em->emrg_env = cl_env_alloc(&em->emrg_refcheck,
340 LCT_REMEMBER|LCT_NOREF);
341 if (!IS_ERR(em->emrg_env))
342 em->emrg_env->le_ctx.lc_cookie = 0x2;
344 result = PTR_ERR(em->emrg_env);
351 lov_emerg_free(emerg, nr);
352 emerg = ERR_PTR(result);
357 static int lov_expand_targets(const struct lu_env *env, struct lov_device *dev)
365 tgt_size = dev->ld_lov->lov_tgt_size;
366 sub_size = dev->ld_target_nr;
367 if (sub_size < tgt_size) {
368 struct lovsub_device **newd;
369 struct lov_device_emerg **emerg;
370 const size_t sz = sizeof newd[0];
372 emerg = lov_emerg_alloc(tgt_size);
374 RETURN(PTR_ERR(emerg));
376 OBD_ALLOC(newd, tgt_size * sz);
378 mutex_lock(&dev->ld_mutex);
380 memcpy(newd, dev->ld_target, sub_size * sz);
381 OBD_FREE(dev->ld_target, sub_size * sz);
383 dev->ld_target = newd;
384 dev->ld_target_nr = tgt_size;
386 if (dev->ld_emrg != NULL)
387 lov_emerg_free(dev->ld_emrg, sub_size);
388 dev->ld_emrg = emerg;
389 mutex_unlock(&dev->ld_mutex);
391 lov_emerg_free(emerg, tgt_size);
398 static int lov_cl_add_target(const struct lu_env *env, struct lu_device *dev,
401 struct obd_device *obd = dev->ld_obd;
402 struct lov_device *ld = lu2lov_dev(dev);
403 struct lov_tgt_desc *tgt;
404 struct lovsub_device *lsd;
405 struct cl_device *cl;
411 tgt = obd->u.lov.lov_tgts[index];
412 LASSERT(tgt != NULL);
413 LASSERT(tgt->ltd_obd != NULL);
415 if (!tgt->ltd_obd->obd_set_up) {
416 CERROR("Target %s not set up\n", obd_uuid2str(&tgt->ltd_uuid));
420 rc = lov_expand_targets(env, ld);
421 if (rc == 0 && ld->ld_flags & LOV_DEV_INITIALIZED) {
422 LASSERT(dev->ld_site != NULL);
424 cl = cl_type_setup(env, dev->ld_site, &lovsub_device_type,
425 tgt->ltd_obd->obd_lu_dev);
427 lsd = cl2lovsub_dev(cl);
428 lsd->acid_idx = index;
429 lsd->acid_super = ld;
430 ld->ld_target[index] = lsd;
432 CERROR("add failed (%d), deleting %s\n", rc,
433 obd_uuid2str(&tgt->ltd_uuid));
434 lov_cl_del_target(env, dev, index);
442 static int lov_process_config(const struct lu_env *env,
443 struct lu_device *d, struct lustre_cfg *cfg)
445 struct obd_device *obd = d->ld_obd;
453 cmd = cfg->lcfg_command;
454 rc = lov_process_config_base(d->ld_obd, cfg, &index, &gen);
457 case LCFG_LOV_ADD_OBD:
458 case LCFG_LOV_ADD_INA:
459 rc = lov_cl_add_target(env, d, index);
461 lov_del_target(d->ld_obd, index, 0, 0);
463 case LCFG_LOV_DEL_OBD:
464 lov_cl_del_target(env, d, index);
472 static const struct lu_device_operations lov_lu_ops = {
473 .ldo_object_alloc = lov_object_alloc,
474 .ldo_process_config = lov_process_config,
477 static struct lu_device *lov_device_alloc(const struct lu_env *env,
478 struct lu_device_type *t,
479 struct lustre_cfg *cfg)
482 struct lov_device *ld;
483 struct obd_device *obd;
488 RETURN(ERR_PTR(-ENOMEM));
490 cl_device_init(&ld->ld_cl, t);
492 d->ld_ops = &lov_lu_ops;
493 ld->ld_cl.cd_ops = &lov_cl_ops;
495 mutex_init(&ld->ld_mutex);
496 lockdep_set_class(&ld->ld_mutex, &cl_lov_device_mutex_class);
498 /* setup the LOV OBD */
499 obd = class_name2obd(lustre_cfg_string(cfg, 0));
500 LASSERT(obd != NULL);
501 rc = lov_setup(obd, cfg);
503 lov_device_free(env, d);
507 ld->ld_lov = &obd->u.lov;
511 static const struct lu_device_type_operations lov_device_type_ops = {
512 .ldto_init = lov_type_init,
513 .ldto_fini = lov_type_fini,
515 .ldto_start = lov_type_start,
516 .ldto_stop = lov_type_stop,
518 .ldto_device_alloc = lov_device_alloc,
519 .ldto_device_free = lov_device_free,
521 .ldto_device_init = lov_device_init,
522 .ldto_device_fini = lov_device_fini
525 struct lu_device_type lov_device_type = {
526 .ldt_tags = LU_DEVICE_CL,
527 .ldt_name = LUSTRE_LOV_NAME,
528 .ldt_ops = &lov_device_type_ops,
529 .ldt_ctx_tags = LCT_CL_THREAD
531 EXPORT_SYMBOL(lov_device_type);