Whamcloud - gitweb
land clio.
[fs/lustre-release.git] / lustre / cmm / cmm_device.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/cmm/cmm_device.c
37  *
38  * Lustre Cluster Metadata Manager (cmm)
39  *
40  * Author: Mike Pershin <tappro@clusterfs.com>
41  */
42
43 #ifndef EXPORT_SYMTAB
44 # define EXPORT_SYMTAB
45 #endif
46 #define DEBUG_SUBSYSTEM S_MDS
47
48 #include <linux/module.h>
49
50 #include <obd.h>
51 #include <obd_class.h>
52 #include <lprocfs_status.h>
53 #include <lustre_ver.h>
54 #include "cmm_internal.h"
55 #include "mdc_internal.h"
56
57 static struct obd_ops cmm_obd_device_ops = {
58         .o_owner           = THIS_MODULE
59 };
60
61 static const struct lu_device_operations cmm_lu_ops;
62
63 static inline int lu_device_is_cmm(struct lu_device *d)
64 {
65         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &cmm_lu_ops);
66 }
67
68 int cmm_root_get(const struct lu_env *env, struct md_device *md,
69                  struct lu_fid *fid)
70 {
71         struct cmm_device *cmm_dev = md2cmm_dev(md);
72         /* valid only on master MDS */
73         if (cmm_dev->cmm_local_num == 0)
74                 return cmm_child_ops(cmm_dev)->mdo_root_get(env,
75                                      cmm_dev->cmm_child, fid);
76         else
77                 return -EINVAL;
78 }
79
80 static int cmm_statfs(const struct lu_env *env, struct md_device *md,
81                       struct kstatfs *sfs)
82 {
83         struct cmm_device *cmm_dev = md2cmm_dev(md);
84         int rc;
85
86         ENTRY;
87         rc = cmm_child_ops(cmm_dev)->mdo_statfs(env,
88                                                 cmm_dev->cmm_child, sfs);
89         RETURN (rc);
90 }
91
92 static int cmm_maxsize_get(const struct lu_env *env, struct md_device *md,
93                            int *md_size, int *cookie_size)
94 {
95         struct cmm_device *cmm_dev = md2cmm_dev(md);
96         int rc;
97         ENTRY;
98         rc = cmm_child_ops(cmm_dev)->mdo_maxsize_get(env, cmm_dev->cmm_child,
99                                                      md_size, cookie_size);
100         RETURN(rc);
101 }
102
103 static int cmm_init_capa_ctxt(const struct lu_env *env, struct md_device *md,
104                               int mode , unsigned long timeout, __u32 alg,
105                               struct lustre_capa_key *keys)
106 {
107         struct cmm_device *cmm_dev = md2cmm_dev(md);
108         int rc;
109         ENTRY;
110         LASSERT(cmm_child_ops(cmm_dev)->mdo_init_capa_ctxt);
111         rc = cmm_child_ops(cmm_dev)->mdo_init_capa_ctxt(env, cmm_dev->cmm_child,
112                                                         mode, timeout, alg,
113                                                         keys);
114         RETURN(rc);
115 }
116
117 static int cmm_update_capa_key(const struct lu_env *env,
118                                struct md_device *md,
119                                struct lustre_capa_key *key)
120 {
121         struct cmm_device *cmm_dev = md2cmm_dev(md);
122         int rc;
123         ENTRY;
124         rc = cmm_child_ops(cmm_dev)->mdo_update_capa_key(env,
125                                                          cmm_dev->cmm_child,
126                                                          key);
127         RETURN(rc);
128 }
129
130 static const struct md_device_operations cmm_md_ops = {
131         .mdo_statfs          = cmm_statfs,
132         .mdo_root_get        = cmm_root_get,
133         .mdo_maxsize_get     = cmm_maxsize_get,
134         .mdo_init_capa_ctxt  = cmm_init_capa_ctxt,
135         .mdo_update_capa_key = cmm_update_capa_key,
136 };
137
138 extern struct lu_device_type mdc_device_type;
139
140 static int cmm_post_init_mdc(const struct lu_env *env,
141                              struct cmm_device *cmm)
142 {
143         int max_mdsize, max_cookiesize, rc;
144         struct mdc_device *mc, *tmp;
145
146         /* get the max mdsize and cookiesize from lower layer */
147         rc = cmm_maxsize_get(env, &cmm->cmm_md_dev, &max_mdsize,
148                                                 &max_cookiesize);
149         if (rc)
150                 RETURN(rc);
151
152         spin_lock(&cmm->cmm_tgt_guard);
153         list_for_each_entry_safe(mc, tmp, &cmm->cmm_targets,
154                                  mc_linkage) {
155                 cmm_mdc_init_ea_size(env, mc, max_mdsize, max_cookiesize);
156         }
157         spin_unlock(&cmm->cmm_tgt_guard);
158         RETURN(rc);
159 }
160
161 /* --- cmm_lu_operations --- */
162 /* add new MDC to the CMM, create MDC lu_device and connect it to mdc_obd */
163 static int cmm_add_mdc(const struct lu_env *env,
164                        struct cmm_device *cm, struct lustre_cfg *cfg)
165 {
166         struct lu_device_type *ldt = &mdc_device_type;
167         char *p, *num = lustre_cfg_string(cfg, 2);
168         struct mdc_device *mc, *tmp;
169         struct lu_fld_target target;
170         struct lu_device *ld;
171         struct lu_device *cmm_lu = cmm2lu_dev(cm);
172         mdsno_t mdc_num;
173         int rc;
174         ENTRY;
175
176         /* find out that there is no such mdc */
177         LASSERT(num);
178         mdc_num = simple_strtol(num, &p, 10);
179         if (*p) {
180                 CERROR("Invalid index in lustre_cgf, offset 2\n");
181                 RETURN(-EINVAL);
182         }
183
184         spin_lock(&cm->cmm_tgt_guard);
185         list_for_each_entry_safe(mc, tmp, &cm->cmm_targets,
186                                  mc_linkage) {
187                 if (mc->mc_num == mdc_num) {
188                         spin_unlock(&cm->cmm_tgt_guard);
189                         RETURN(-EEXIST);
190                 }
191         }
192         spin_unlock(&cm->cmm_tgt_guard);
193         ld = ldt->ldt_ops->ldto_device_alloc(env, ldt, cfg);
194         if (IS_ERR(ld))
195                 RETURN(PTR_ERR(ld));
196
197         ld->ld_site = cmm2lu_dev(cm)->ld_site;
198
199         rc = ldt->ldt_ops->ldto_device_init(env, ld, NULL, NULL);
200         if (rc) {
201                 ldt->ldt_ops->ldto_device_free(env, ld);
202                 RETURN(rc);
203         }
204         /* pass config to the just created MDC */
205         rc = ld->ld_ops->ldo_process_config(env, ld, cfg);
206         if (rc) {
207                 ldt->ldt_ops->ldto_device_fini(env, ld);
208                 ldt->ldt_ops->ldto_device_free(env, ld);
209                 RETURN(rc);
210         }
211
212         spin_lock(&cm->cmm_tgt_guard);
213         list_for_each_entry_safe(mc, tmp, &cm->cmm_targets,
214                                  mc_linkage) {
215                 if (mc->mc_num == mdc_num) {
216                         spin_unlock(&cm->cmm_tgt_guard);
217                         ldt->ldt_ops->ldto_device_fini(env, ld);
218                         ldt->ldt_ops->ldto_device_free(env, ld);
219                         RETURN(-EEXIST);
220                 }
221         }
222         mc = lu2mdc_dev(ld);
223         list_add_tail(&mc->mc_linkage, &cm->cmm_targets);
224         cm->cmm_tgt_count++;
225         spin_unlock(&cm->cmm_tgt_guard);
226
227         lu_device_get(cmm_lu);
228         lu_ref_add(&cmm_lu->ld_reference, "mdc-child", ld);
229
230         target.ft_srv = NULL;
231         target.ft_idx = mc->mc_num;
232         target.ft_exp = mc->mc_desc.cl_exp;
233         fld_client_add_target(cm->cmm_fld, &target);
234
235         /* Set max md size for the mdc. */
236         rc = cmm_post_init_mdc(env, cm);
237         RETURN(rc);
238 }
239
240 static void cmm_device_shutdown(const struct lu_env *env,
241                                 struct cmm_device *cm,
242                                 struct lustre_cfg *cfg)
243 {
244         struct mdc_device *mc, *tmp;
245         ENTRY;
246
247         /* Remove local target from FLD. */
248         fld_client_del_target(cm->cmm_fld, cm->cmm_local_num);
249
250         /* Finish all mdc devices. */
251         spin_lock(&cm->cmm_tgt_guard);
252         list_for_each_entry_safe(mc, tmp, &cm->cmm_targets, mc_linkage) {
253                 struct lu_device *ld_m = mdc2lu_dev(mc);
254                 fld_client_del_target(cm->cmm_fld, mc->mc_num);
255                 ld_m->ld_ops->ldo_process_config(env, ld_m, cfg);
256         }
257         spin_unlock(&cm->cmm_tgt_guard);
258
259         /* remove upcall device*/
260         md_upcall_fini(&cm->cmm_md_dev);
261
262         EXIT;
263 }
264
265 static int cmm_device_mount(const struct lu_env *env,
266                             struct cmm_device *m, struct lustre_cfg *cfg)
267 {
268         const char *index = lustre_cfg_string(cfg, 2);
269         char *p;
270
271         LASSERT(index != NULL);
272
273         m->cmm_local_num = simple_strtol(index, &p, 10);
274         if (*p) {
275                 CERROR("Invalid index in lustre_cgf\n");
276                 RETURN(-EINVAL);
277         }
278
279         RETURN(0);
280 }
281
282 static int cmm_process_config(const struct lu_env *env,
283                               struct lu_device *d, struct lustre_cfg *cfg)
284 {
285         struct cmm_device *m = lu2cmm_dev(d);
286         struct lu_device *next = md2lu_dev(m->cmm_child);
287         int err;
288         ENTRY;
289
290         switch(cfg->lcfg_command) {
291         case LCFG_ADD_MDC:
292                 /* On first ADD_MDC add also local target. */
293                 if (!(m->cmm_flags & CMM_INITIALIZED)) {
294                         struct lu_site *ls = cmm2lu_dev(m)->ld_site;
295                         struct lu_fld_target target;
296
297                         target.ft_srv = lu_site2md(ls)->ms_server_fld;
298                         target.ft_idx = m->cmm_local_num;
299                         target.ft_exp = NULL;
300
301                         fld_client_add_target(m->cmm_fld, &target);
302                 }
303                 err = cmm_add_mdc(env, m, cfg);
304
305                 /* The first ADD_MDC can be counted as setup is finished. */
306                 if (!(m->cmm_flags & CMM_INITIALIZED))
307                         m->cmm_flags |= CMM_INITIALIZED;
308
309                 break;
310         case LCFG_SETUP:
311         {
312                 /* lower layers should be set up at first */
313                 err = next->ld_ops->ldo_process_config(env, next, cfg);
314                 if (err == 0)
315                         err = cmm_device_mount(env, m, cfg);
316                 break;
317         }
318         case LCFG_CLEANUP:
319         {
320                 cmm_device_shutdown(env, m, cfg);
321         }
322         default:
323                 err = next->ld_ops->ldo_process_config(env, next, cfg);
324         }
325         RETURN(err);
326 }
327
328 static int cmm_recovery_complete(const struct lu_env *env,
329                                  struct lu_device *d)
330 {
331         struct cmm_device *m = lu2cmm_dev(d);
332         struct lu_device *next = md2lu_dev(m->cmm_child);
333         int rc;
334         ENTRY;
335         rc = next->ld_ops->ldo_recovery_complete(env, next);
336         RETURN(rc);
337 }
338
339 static const struct lu_device_operations cmm_lu_ops = {
340         .ldo_object_alloc      = cmm_object_alloc,
341         .ldo_process_config    = cmm_process_config,
342         .ldo_recovery_complete = cmm_recovery_complete
343 };
344
345 /* --- lu_device_type operations --- */
346 int cmm_upcall(const struct lu_env *env, struct md_device *md,
347                enum md_upcall_event ev)
348 {
349         int rc;
350         ENTRY;
351
352         switch (ev) {
353                 case MD_LOV_SYNC:
354                         rc = cmm_post_init_mdc(env, md2cmm_dev(md));
355                         if (rc)
356                                 CERROR("can not init md size %d\n", rc);
357                         /* fall through */
358                 default:
359                         rc = md_do_upcall(env, md, ev);
360         }
361         RETURN(rc);
362 }
363
364 static struct lu_device *cmm_device_free(const struct lu_env *env,
365                                          struct lu_device *d)
366 {
367         struct cmm_device *m = lu2cmm_dev(d);
368         struct lu_device  *next = md2lu_dev(m->cmm_child);
369         ENTRY;
370
371         LASSERT(m->cmm_tgt_count == 0);
372         LASSERT(list_empty(&m->cmm_targets));
373         if (m->cmm_fld != NULL) {
374                 OBD_FREE_PTR(m->cmm_fld);
375                 m->cmm_fld = NULL;
376         }
377         md_device_fini(&m->cmm_md_dev);
378         OBD_FREE_PTR(m);
379         RETURN(next);
380 }
381
382 static struct lu_device *cmm_device_alloc(const struct lu_env *env,
383                                           struct lu_device_type *t,
384                                           struct lustre_cfg *cfg)
385 {
386         struct lu_device  *l;
387         struct cmm_device *m;
388         ENTRY;
389
390         OBD_ALLOC_PTR(m);
391         if (m == NULL) {
392                 l = ERR_PTR(-ENOMEM);
393         } else {
394                 md_device_init(&m->cmm_md_dev, t);
395                 m->cmm_md_dev.md_ops = &cmm_md_ops;
396                 md_upcall_init(&m->cmm_md_dev, cmm_upcall);
397                 l = cmm2lu_dev(m);
398                 l->ld_ops = &cmm_lu_ops;
399
400                 OBD_ALLOC_PTR(m->cmm_fld);
401                 if (!m->cmm_fld) {
402                         cmm_device_free(env, l);
403                         l = ERR_PTR(-ENOMEM);
404                 }
405         }
406         RETURN(l);
407 }
408
409 /* context key constructor/destructor: cmm_key_init, cmm_key_fini */
410 LU_KEY_INIT_FINI(cmm, struct cmm_thread_info);
411
412 /* context key: cmm_thread_key */
413 LU_CONTEXT_KEY_DEFINE(cmm, LCT_MD_THREAD);
414
415 struct cmm_thread_info *cmm_env_info(const struct lu_env *env)
416 {
417         struct cmm_thread_info *info;
418
419         info = lu_context_key_get(&env->le_ctx, &cmm_thread_key);
420         LASSERT(info != NULL);
421         return info;
422 }
423
424 /* type constructor/destructor: cmm_type_init/cmm_type_fini */
425 LU_TYPE_INIT_FINI(cmm, &cmm_thread_key);
426
427 static int cmm_device_init(const struct lu_env *env, struct lu_device *d,
428                            const char *name, struct lu_device *next)
429 {
430         struct cmm_device *m = lu2cmm_dev(d);
431         struct lu_site *ls;
432         int err = 0;
433         ENTRY;
434
435         spin_lock_init(&m->cmm_tgt_guard);
436         CFS_INIT_LIST_HEAD(&m->cmm_targets);
437         m->cmm_tgt_count = 0;
438         m->cmm_child = lu2md_dev(next);
439
440         err = fld_client_init(m->cmm_fld, name,
441                               LUSTRE_CLI_FLD_HASH_DHT);
442         if (err) {
443                 CERROR("Can't init FLD, err %d\n", err);
444                 RETURN(err);
445         }
446
447         /* Assign site's fld client ref, needed for asserts in osd. */
448         ls = cmm2lu_dev(m)->ld_site;
449         lu_site2md(ls)->ms_client_fld = m->cmm_fld;
450         err = cmm_procfs_init(m, name);
451
452         RETURN(err);
453 }
454
455 static struct lu_device *cmm_device_fini(const struct lu_env *env,
456                                          struct lu_device *ld)
457 {
458         struct cmm_device *cm = lu2cmm_dev(ld);
459         struct mdc_device *mc, *tmp;
460         struct lu_site *ls;
461         ENTRY;
462
463         /* Finish all mdc devices */
464         spin_lock(&cm->cmm_tgt_guard);
465         list_for_each_entry_safe(mc, tmp, &cm->cmm_targets, mc_linkage) {
466                 struct lu_device *ld_m = mdc2lu_dev(mc);
467                 struct lu_device *ld_c = cmm2lu_dev(cm);
468
469                 list_del_init(&mc->mc_linkage);
470                 lu_ref_del(&ld_c->ld_reference, "mdc-child", ld_m);
471                 lu_device_put(ld_c);
472                 ld_m->ld_type->ldt_ops->ldto_device_fini(env, ld_m);
473                 ld_m->ld_type->ldt_ops->ldto_device_free(env, ld_m);
474                 cm->cmm_tgt_count--;
475         }
476         spin_unlock(&cm->cmm_tgt_guard);
477
478         fld_client_fini(cm->cmm_fld);
479         ls = cmm2lu_dev(cm)->ld_site;
480         lu_site2md(ls)->ms_client_fld = NULL;
481         cmm_procfs_fini(cm);
482
483         RETURN (md2lu_dev(cm->cmm_child));
484 }
485
486 static struct lu_device_type_operations cmm_device_type_ops = {
487         .ldto_init = cmm_type_init,
488         .ldto_fini = cmm_type_fini,
489
490         .ldto_start = cmm_type_start,
491         .ldto_stop  = cmm_type_stop,
492
493         .ldto_device_alloc = cmm_device_alloc,
494         .ldto_device_free  = cmm_device_free,
495
496         .ldto_device_init = cmm_device_init,
497         .ldto_device_fini = cmm_device_fini
498 };
499
500 static struct lu_device_type cmm_device_type = {
501         .ldt_tags     = LU_DEVICE_MD,
502         .ldt_name     = LUSTRE_CMM_NAME,
503         .ldt_ops      = &cmm_device_type_ops,
504         .ldt_ctx_tags = LCT_MD_THREAD | LCT_DT_THREAD
505 };
506
507 struct lprocfs_vars lprocfs_cmm_obd_vars[] = {
508         { 0 }
509 };
510
511 struct lprocfs_vars lprocfs_cmm_module_vars[] = {
512         { 0 }
513 };
514
515 static void lprocfs_cmm_init_vars(struct lprocfs_static_vars *lvars)
516 {
517     lvars->module_vars  = lprocfs_cmm_module_vars;
518     lvars->obd_vars     = lprocfs_cmm_obd_vars;
519 }
520
521 static int __init cmm_mod_init(void)
522 {
523         struct lprocfs_static_vars lvars;
524
525         lprocfs_cmm_init_vars(&lvars);
526         return class_register_type(&cmm_obd_device_ops, NULL, lvars.module_vars,
527                                    LUSTRE_CMM_NAME, &cmm_device_type);
528 }
529
530 static void __exit cmm_mod_exit(void)
531 {
532         class_unregister_type(LUSTRE_CMM_NAME);
533 }
534
535 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
536 MODULE_DESCRIPTION("Lustre Clustered Metadata Manager ("LUSTRE_CMM_NAME")");
537 MODULE_LICENSE("GPL");
538
539 cfs_module(cmm, "0.1.0", cmm_mod_init, cmm_mod_exit);