1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
30 * Use is subject to license terms.
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_page_kmem;
49 cfs_mem_cache_t *lov_lock_kmem;
50 cfs_mem_cache_t *lov_object_kmem;
51 cfs_mem_cache_t *lov_thread_kmem;
52 cfs_mem_cache_t *lov_session_kmem;
53 cfs_mem_cache_t *lov_req_kmem;
55 cfs_mem_cache_t *lovsub_page_kmem;
56 cfs_mem_cache_t *lovsub_lock_kmem;
57 cfs_mem_cache_t *lovsub_object_kmem;
58 cfs_mem_cache_t *lovsub_req_kmem;
60 cfs_mem_cache_t *lov_lock_link_kmem;
62 /** Lock class of lov_device::ld_mutex. */
63 struct lock_class_key cl_lov_device_mutex_class;
65 struct lu_kmem_descr lov_caches[] = {
67 .ckd_cache = &lov_page_kmem,
68 .ckd_name = "lov_page_kmem",
69 .ckd_size = sizeof (struct lov_page)
72 .ckd_cache = &lov_lock_kmem,
73 .ckd_name = "lov_lock_kmem",
74 .ckd_size = sizeof (struct lov_lock)
77 .ckd_cache = &lov_object_kmem,
78 .ckd_name = "lov_object_kmem",
79 .ckd_size = sizeof (struct lov_object)
82 .ckd_cache = &lov_thread_kmem,
83 .ckd_name = "lov_thread_kmem",
84 .ckd_size = sizeof (struct lov_thread_info)
87 .ckd_cache = &lov_session_kmem,
88 .ckd_name = "lov_session_kmem",
89 .ckd_size = sizeof (struct lov_session)
92 .ckd_cache = &lov_req_kmem,
93 .ckd_name = "lov_req_kmem",
94 .ckd_size = sizeof (struct lov_req)
97 .ckd_cache = &lovsub_page_kmem,
98 .ckd_name = "lovsub_page_kmem",
99 .ckd_size = sizeof (struct lovsub_page)
102 .ckd_cache = &lovsub_lock_kmem,
103 .ckd_name = "lovsub_lock_kmem",
104 .ckd_size = sizeof (struct lovsub_lock)
107 .ckd_cache = &lovsub_object_kmem,
108 .ckd_name = "lovsub_object_kmem",
109 .ckd_size = sizeof (struct lovsub_object)
112 .ckd_cache = &lovsub_req_kmem,
113 .ckd_name = "lovsub_req_kmem",
114 .ckd_size = sizeof (struct lovsub_req)
117 .ckd_cache = &lov_lock_link_kmem,
118 .ckd_name = "lov_lock_link_kmem",
119 .ckd_size = sizeof (struct lov_lock_link)
126 /*****************************************************************************
128 * Lov transfer operations.
132 static void lov_req_completion(const struct lu_env *env,
133 const struct cl_req_slice *slice, int ioret)
138 lr = cl2lov_req(slice);
139 OBD_SLAB_FREE_PTR(lr, lov_req_kmem);
143 static const struct cl_req_operations lov_req_ops = {
144 .cro_completion = lov_req_completion
147 /*****************************************************************************
149 * Lov device and device type functions.
153 static void *lov_key_init(const struct lu_context *ctx,
154 struct lu_context_key *key)
156 struct lov_thread_info *info;
158 OBD_SLAB_ALLOC_PTR(info, lov_thread_kmem);
160 CFS_INIT_LIST_HEAD(&info->lti_closure.clc_list);
162 info = ERR_PTR(-ENOMEM);
166 static void lov_key_fini(const struct lu_context *ctx,
167 struct lu_context_key *key, void *data)
169 struct lov_thread_info *info = data;
170 LINVRNT(list_empty(&info->lti_closure.clc_list));
171 OBD_SLAB_FREE_PTR(info, lov_thread_kmem);
174 struct lu_context_key lov_key = {
175 .lct_tags = LCT_CL_THREAD,
176 .lct_init = lov_key_init,
177 .lct_fini = lov_key_fini
180 static void *lov_session_key_init(const struct lu_context *ctx,
181 struct lu_context_key *key)
183 struct lov_session *info;
185 OBD_SLAB_ALLOC_PTR(info, lov_session_kmem);
187 info = ERR_PTR(-ENOMEM);
191 static void lov_session_key_fini(const struct lu_context *ctx,
192 struct lu_context_key *key, void *data)
194 struct lov_session *info = data;
195 OBD_SLAB_FREE_PTR(info, lov_session_kmem);
198 struct lu_context_key lov_session_key = {
199 .lct_tags = LCT_SESSION,
200 .lct_init = lov_session_key_init,
201 .lct_fini = lov_session_key_fini
204 /* type constructor/destructor: lov_type_{init,fini,start,stop}() */
205 LU_TYPE_INIT_FINI(lov, &lov_key, &lov_session_key);
207 static struct lu_device *lov_device_fini(const struct lu_env *env,
211 struct lov_device *ld = lu2lov_dev(d);
213 LASSERT(ld->ld_lov != NULL);
214 if (ld->ld_target == NULL)
217 lov_foreach_target(ld, i) {
218 struct lovsub_device *lsd;
220 lsd = ld->ld_target[i];
222 cl_stack_fini(env, lovsub2cl_dev(lsd));
223 ld->ld_target[i] = NULL;
229 static int lov_device_init(const struct lu_env *env, struct lu_device *d,
230 const char *name, struct lu_device *next)
232 struct lov_device *ld = lu2lov_dev(d);
236 LASSERT(d->ld_site != NULL);
237 if (ld->ld_target == NULL)
240 lov_foreach_target(ld, i) {
241 struct lovsub_device *lsd;
242 struct cl_device *cl;
243 struct lov_tgt_desc *desc;
245 desc = ld->ld_lov->lov_tgts[i];
246 if (desc->ltd_active) {
247 cl = cl_type_setup(env, d->ld_site, &lovsub_device_type,
248 desc->ltd_exp->exp_obd->obd_lu_dev);
253 lsd = cl2lovsub_dev(cl);
255 lsd->acid_super = ld;
256 ld->ld_target[i] = lsd;
261 lov_device_fini(env, d);
263 ld->ld_flags |= LOV_DEV_INITIALIZED;
268 static int lov_req_init(const struct lu_env *env, struct cl_device *dev,
275 OBD_SLAB_ALLOC_PTR(lr, lov_req_kmem);
277 cl_req_slice_add(req, &lr->lr_cl, dev, &lov_req_ops);
284 static const struct cl_device_operations lov_cl_ops = {
285 .cdo_req_init = lov_req_init
288 static void lov_emerg_free(struct lov_device_emerg **emrg, int nr)
292 for (i = 0; i < nr; ++i) {
293 struct lov_device_emerg *em;
297 LASSERT(em->emrg_page_list.pl_nr == 0);
298 if (em->emrg_env != NULL)
299 cl_env_put(em->emrg_env, &em->emrg_refcheck);
303 OBD_FREE(emrg, nr * sizeof emrg[0]);
306 static struct lu_device *lov_device_free(const struct lu_env *env,
309 struct lov_device *ld = lu2lov_dev(d);
310 const int nr = ld->ld_target_nr;
312 cl_device_fini(lu2cl_dev(d));
313 if (ld->ld_target != NULL)
314 OBD_FREE(ld->ld_target, nr * sizeof ld->ld_target[0]);
315 if (ld->ld_emrg != NULL)
316 lov_emerg_free(ld->ld_emrg, nr);
321 static void lov_cl_del_target(const struct lu_env *env, struct lu_device *dev,
324 struct lov_device *ld = lu2lov_dev(dev);
327 if (ld->ld_target[index] != NULL) {
328 cl_stack_fini(env, lovsub2cl_dev(ld->ld_target[index]));
329 ld->ld_target[index] = NULL;
334 static struct lov_device_emerg **lov_emerg_alloc(int nr)
336 struct lov_device_emerg **emerg;
340 OBD_ALLOC(emerg, nr * sizeof emerg[0]);
342 return ERR_PTR(-ENOMEM);
343 for (result = i = 0; i < nr && result == 0; i++) {
344 struct lov_device_emerg *em;
350 cl_page_list_init(&em->emrg_page_list);
351 cookie = cl_env_reenter();
352 em->emrg_env = cl_env_alloc(&em->emrg_refcheck,
353 LCT_REMEMBER|LCT_NOREF);
354 cl_env_reexit(cookie);
355 if (!IS_ERR(em->emrg_env))
356 em->emrg_env->le_ctx.lc_cookie = 0x2;
358 result = PTR_ERR(em->emrg_env);
365 lov_emerg_free(emerg, nr);
366 emerg = ERR_PTR(result);
371 static int lov_expand_targets(const struct lu_env *env, struct lov_device *dev)
379 tgt_size = dev->ld_lov->lov_tgt_size;
380 sub_size = dev->ld_target_nr;
381 if (sub_size < tgt_size) {
382 struct lovsub_device **newd;
383 struct lov_device_emerg **emerg;
384 const size_t sz = sizeof newd[0];
386 emerg = lov_emerg_alloc(tgt_size);
388 RETURN(PTR_ERR(emerg));
390 OBD_ALLOC(newd, tgt_size * sz);
392 mutex_lock(&dev->ld_mutex);
394 memcpy(newd, dev->ld_target, sub_size * sz);
395 OBD_FREE(dev->ld_target, sub_size * sz);
397 dev->ld_target = newd;
398 dev->ld_target_nr = tgt_size;
400 if (dev->ld_emrg != NULL)
401 lov_emerg_free(dev->ld_emrg, sub_size);
402 dev->ld_emrg = emerg;
403 mutex_unlock(&dev->ld_mutex);
405 lov_emerg_free(emerg, tgt_size);
412 static int lov_cl_add_target(const struct lu_env *env, struct lu_device *dev,
415 struct obd_device *obd = dev->ld_obd;
416 struct lov_device *ld = lu2lov_dev(dev);
417 struct lov_tgt_desc *tgt;
418 struct lovsub_device *lsd;
419 struct cl_device *cl;
425 tgt = obd->u.lov.lov_tgts[index];
426 LASSERT(tgt != NULL);
428 rc = lov_expand_targets(env, ld);
429 if (rc == 0 && ld->ld_flags & LOV_DEV_INITIALIZED) {
430 LASSERT(dev->ld_site != NULL);
431 cl = cl_type_setup(env, dev->ld_site, &lovsub_device_type,
432 tgt->ltd_exp->exp_obd->obd_lu_dev);
434 lsd = cl2lovsub_dev(cl);
435 lsd->acid_idx = index;
436 lsd->acid_super = ld;
437 ld->ld_target[index] = lsd;
439 CERROR("add failed (%d), deleting %s\n", rc,
440 obd_uuid2str(&tgt->ltd_uuid));
441 lov_cl_del_target(env, dev, index);
449 static int lov_process_config(const struct lu_env *env,
450 struct lu_device *d, struct lustre_cfg *cfg)
452 struct obd_device *obd = d->ld_obd;
460 cmd = cfg->lcfg_command;
461 rc = lov_process_config_base(d->ld_obd, cfg, &index, &gen);
464 case LCFG_LOV_ADD_OBD:
465 case LCFG_LOV_ADD_INA:
466 rc = lov_cl_add_target(env, d, index);
468 lov_del_target(d->ld_obd, index, 0, 0);
470 case LCFG_LOV_DEL_OBD:
471 lov_cl_del_target(env, d, index);
479 static const struct lu_device_operations lov_lu_ops = {
480 .ldo_object_alloc = lov_object_alloc,
481 .ldo_process_config = lov_process_config,
484 static struct lu_device *lov_device_alloc(const struct lu_env *env,
485 struct lu_device_type *t,
486 struct lustre_cfg *cfg)
489 struct lov_device *ld;
490 struct obd_device *obd;
495 RETURN(ERR_PTR(-ENOMEM));
497 cl_device_init(&ld->ld_cl, t);
499 d->ld_ops = &lov_lu_ops;
500 ld->ld_cl.cd_ops = &lov_cl_ops;
502 mutex_init(&ld->ld_mutex);
503 lockdep_set_class(&ld->ld_mutex, &cl_lov_device_mutex_class);
505 /* setup the LOV OBD */
506 obd = class_name2obd(lustre_cfg_string(cfg, 0));
507 LASSERT(obd != NULL);
508 rc = lov_setup(obd, cfg);
510 lov_device_free(env, d);
514 ld->ld_lov = &obd->u.lov;
518 static const struct lu_device_type_operations lov_device_type_ops = {
519 .ldto_init = lov_type_init,
520 .ldto_fini = lov_type_fini,
522 .ldto_start = lov_type_start,
523 .ldto_stop = lov_type_stop,
525 .ldto_device_alloc = lov_device_alloc,
526 .ldto_device_free = lov_device_free,
528 .ldto_device_init = lov_device_init,
529 .ldto_device_fini = lov_device_fini
532 struct lu_device_type lov_device_type = {
533 .ldt_tags = LU_DEVICE_CL,
534 .ldt_name = LUSTRE_LOV_NAME,
535 .ldt_ops = &lov_device_type_ops,
536 .ldt_ctx_tags = LCT_CL_THREAD
538 EXPORT_SYMBOL(lov_device_type);