Whamcloud - gitweb
set CMM_INITIALIZED when first ADD_MDC arrives
[fs/lustre-release.git] / lustre / cmm / mdc_device.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/cmm/cmm_mdc.c
5  *  Lustre Metadata Client (mdc)
6  *
7  *  Copyright (c) 2006 Cluster File Systems, Inc.
8  *   Author: Mike Pershin <tappro@clusterfs.com>
9  *
10  *   This file is part of the Lustre file system, http://www.lustre.org
11  *   Lustre is a trademark of Cluster File Systems, Inc.
12  *
13  *   You may have signed or agreed to another license before downloading
14  *   this software.  If so, you are bound by the terms and conditions
15  *   of that agreement, and the following does not apply to you.  See the
16  *   LICENSE file included with this distribution for more information.
17  *
18  *   If you did not agree to a different license, then this copy of Lustre
19  *   is open source software; you can redistribute it and/or modify it
20  *   under the terms of version 2 of the GNU General Public License as
21  *   published by the Free Software Foundation.
22  *
23  *   In either case, Lustre is distributed in the hope that it will be
24  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
25  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  *   license text for more details.
27  */
28
29 #ifndef EXPORT_SYMTAB
30 # define EXPORT_SYMTAB
31 #endif
32 #define DEBUG_SUBSYSTEM S_MDS
33
34 #include <obd.h>
35 #include <obd_class.h>
36 #include <lprocfs_status.h>
37 #include <lustre_ver.h>
38 #include "mdc_internal.h"
39
40 static struct lu_device_operations mdc_lu_ops;
41
42 static inline int lu_device_is_mdc(struct lu_device *ld)
43 {
44         /*
45          * XXX for now. Tags in lu_device_type->ldt_something are needed.
46          */
47         return ergo(ld != NULL && ld->ld_ops != NULL,
48                     ld->ld_ops == &mdc_lu_ops);
49 }
50
51 static struct md_device_operations mdc_md_ops = { 0 };
52
53 /* MDC OBD is set up already and connected to the proper MDS
54  * mdc_add_obd() find that obd by uuid and connects to it.
55  * Local MDT uuid is used for connection
56  * */
57 static int mdc_add_obd(struct mdc_device *mc, struct lustre_cfg *cfg)
58 {
59         struct mdc_cli_desc *desc = &mc->mc_desc;
60         struct obd_device *mdc, *mdt;
61         const char *srv = lustre_cfg_string(cfg, 0);
62         const char *uuid_str = lustre_cfg_string(cfg, 1);
63         const char *index = lustre_cfg_string(cfg, 2);
64         char *p;
65         int rc = 0;
66
67         ENTRY;
68         LASSERT(uuid_str);
69         LASSERT(index);
70         
71         mc->mc_num = simple_strtol(index, &p, 10);
72         if (*p) {
73                 CERROR("Invalid index in lustre_cgf, offset 2\n");                
74                 RETURN(-EINVAL);
75         }
76
77         /* find local MDT obd to get group uuid */
78         mdt = class_name2obd(srv);
79         if (mdt == NULL) {
80                 CERROR("No such OBD %s\n", srv);
81                 LBUG();
82         }
83         obd_str2uuid(&desc->cl_srv_uuid, uuid_str);
84         /* try to find MDC OBD connected to the needed MDT */
85         mdc = class_find_client_obd(&desc->cl_srv_uuid, LUSTRE_MDC_NAME,
86                                     &mdt->obd_uuid);
87         if (!mdc) {
88                 CERROR("Cannot find MDC OBD connected to %s\n", uuid_str);
89                 rc = -ENOENT;
90         } else if (!mdc->obd_set_up) {
91                 CERROR("target %s not set up\n", mdc->obd_name);
92                 rc = -EINVAL;
93         } else {
94                 struct lustre_handle *conn = &desc->cl_conn;
95
96                 CDEBUG(D_CONFIG, "connect to %s(%s)\n",
97                        mdc->obd_name, mdc->obd_uuid.uuid);
98
99                 rc = obd_connect(conn, mdc, &mdt->obd_uuid, NULL);
100
101                 if (rc) {
102                         CERROR("target %s connect error %d\n",
103                                mdc->obd_name, rc);
104                 } else {
105                         desc->cl_exp = class_conn2export(conn);
106                 }
107         }
108
109         RETURN(rc);
110 }
111
112 static int mdc_del_obd(struct mdc_device *mc)
113 {
114         struct mdc_cli_desc *desc = &mc->mc_desc;
115         int rc;
116
117         ENTRY;
118
119         CDEBUG(D_CONFIG, "disconnect from %s\n",
120                class_exp2obd(desc->cl_exp)->obd_name);
121
122         rc = obd_disconnect(desc->cl_exp);
123         if (rc) {
124                 CERROR("target %s disconnect error %d\n",
125                        class_exp2obd(desc->cl_exp)->obd_name, rc);
126         }
127         desc->cl_exp = NULL;
128
129         RETURN(rc);
130 }
131
132 static int mdc_process_config(const struct lu_context *ctx,
133                               struct lu_device *ld, struct lustre_cfg *cfg)
134 {
135         struct mdc_device *mc = lu2mdc_dev(ld);
136         int rc;
137
138         ENTRY;
139         switch (cfg->lcfg_command) {
140         case LCFG_ADD_MDC:
141                 rc = mdc_add_obd(mc, cfg);
142                 break;
143         default:
144                 rc = -EOPNOTSUPP;
145         }
146         RETURN(rc);
147 }
148
149 static struct lu_device_operations mdc_lu_ops = {
150         .ldo_object_alloc   = mdc_object_alloc,
151         .ldo_process_config = mdc_process_config
152 };
153
154 static int mdc_device_init(const struct lu_context *ctx,
155                            struct lu_device *ld, struct lu_device *next)
156 {
157         return 0;
158 }
159
160 static struct lu_device *mdc_device_fini(const struct lu_context *ctx,
161                                          struct lu_device *ld)
162 {
163         struct mdc_device *mc = lu2mdc_dev(ld);
164
165         ENTRY;
166
167         mdc_del_obd(mc);
168
169         RETURN (NULL);
170 }
171
172 struct lu_device *mdc_device_alloc(const struct lu_context *ctx,
173                                    struct lu_device_type *ldt,
174                                    struct lustre_cfg *cfg)
175 {
176         struct lu_device  *ld;
177         struct mdc_device *mc;
178
179         ENTRY;
180
181         OBD_ALLOC_PTR(mc);
182         if (mc == NULL) {
183                 ld = ERR_PTR(-ENOMEM);
184         } else {
185                 md_device_init(&mc->mc_md_dev, ldt);
186                 mc->mc_md_dev.md_ops =  &mdc_md_ops;
187                 ld = mdc2lu_dev(mc);
188                 ld->ld_ops = &mdc_lu_ops;
189         }
190
191         RETURN (ld);
192 }
193 void mdc_device_free(const struct lu_context *ctx, struct lu_device *ld)
194 {
195         struct mdc_device *mc = lu2mdc_dev(ld);
196
197         LASSERT(atomic_read(&ld->ld_ref) == 0);
198         LASSERT(list_empty(&mc->mc_linkage));
199         md_device_fini(&mc->mc_md_dev);
200         OBD_FREE_PTR(mc);
201 }
202
203 /* context key constructor/destructor */
204
205 static void *mdc_thread_init(const struct lu_context *ctx,
206                              struct lu_context_key *key)
207 {
208         struct mdc_thread_info *info;
209
210         CLASSERT(CFS_PAGE_SIZE >= sizeof *info);
211         OBD_ALLOC_PTR(info);
212         if (info == NULL)
213                 info = ERR_PTR(-ENOMEM);
214         return info;
215 }
216
217 static void mdc_thread_fini(const struct lu_context *ctx,
218                             struct lu_context_key *key, void *data)
219 {
220         struct mdc_thread_info *info = data;
221         OBD_FREE_PTR(info);
222 }
223
224 struct lu_context_key mdc_thread_key = {
225         .lct_init = mdc_thread_init,
226         .lct_fini = mdc_thread_fini
227 };
228
229 int mdc_type_init(struct lu_device_type *ldt)
230 {
231         return lu_context_key_register(&mdc_thread_key);
232 }
233
234 void mdc_type_fini(struct lu_device_type *ldt)
235 {
236         lu_context_key_degister(&mdc_thread_key);
237 }
238
239 static struct lu_device_type_operations mdc_device_type_ops = {
240         .ldto_init = mdc_type_init,
241         .ldto_fini = mdc_type_fini,
242
243         .ldto_device_alloc = mdc_device_alloc,
244         .ldto_device_free  = mdc_device_free,
245
246         .ldto_device_init = mdc_device_init,
247         .ldto_device_fini = mdc_device_fini
248 };
249
250 struct lu_device_type mdc_device_type = {
251         .ldt_tags = LU_DEVICE_MD,
252         .ldt_name = LUSTRE_MDC0_NAME,
253         .ldt_ops  = &mdc_device_type_ops
254 };
255