Whamcloud - gitweb
- make compiler happy with initialized value
[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 "cmm_internal.h"
39 #include "mdc_internal.h"
40
41 static struct lu_device_operations mdc_lu_ops;
42
43 static inline int lu_device_is_mdc(struct lu_device *ld)
44 {
45         return ergo(ld != NULL && ld->ld_ops != NULL,
46                     ld->ld_ops == &mdc_lu_ops);
47 }
48
49 static struct md_device_operations mdc_md_ops = { 0 };
50
51 static int mdc_obd_update(struct obd_device *host,
52                           struct obd_device *watched,
53                           enum obd_notify_event ev, void *owner)
54 {
55         struct mdc_device *mc = owner;
56         int rc = 0;
57         ENTRY;
58
59         LASSERT(mc != NULL);
60         CDEBUG(D_CONFIG, "notify %s ev=%d\n", watched->obd_name, ev);
61         RETURN(rc);
62 }
63 /* MDC OBD is set up already and connected to the proper MDS
64  * mdc_add_obd() find that obd by uuid and connects to it.
65  * Local MDT uuid is used for connection
66  * */
67 static int mdc_obd_add(const struct lu_env *env,
68                        struct mdc_device *mc, struct lustre_cfg *cfg)
69 {
70         struct mdc_cli_desc *desc = &mc->mc_desc;
71         struct obd_device *mdc;
72         const char *uuid_str = lustre_cfg_string(cfg, 1);
73         const char *index = lustre_cfg_string(cfg, 2);
74         const char *mdc_uuid_str = lustre_cfg_string(cfg, 4);
75         char *p;
76         int rc = 0;
77
78         ENTRY;
79         LASSERT(uuid_str);
80         LASSERT(index);
81
82         mc->mc_num = simple_strtol(index, &p, 10);
83         if (*p) {
84                 CERROR("Invalid index in lustre_cgf, offset 2\n");
85                 RETURN(-EINVAL);
86         }
87
88         obd_str2uuid(&desc->cl_srv_uuid, uuid_str);
89         obd_str2uuid(&desc->cl_cli_uuid, mdc_uuid_str);
90         /* try to find MDC OBD connected to the needed MDT */
91         mdc = class_find_client_obd(&desc->cl_srv_uuid, LUSTRE_MDC_NAME,
92                                     &desc->cl_cli_uuid);
93         if (!mdc) {
94                 CERROR("Cannot find MDC OBD connected to %s\n", uuid_str);
95                 rc = -ENOENT;
96         } else if (!mdc->obd_set_up) {
97                 CERROR("target %s not set up\n", mdc->obd_name);
98                 rc = -EINVAL;
99         } else {
100                 struct lustre_handle *conn = &desc->cl_conn;
101                 struct obd_connect_data *ocd;
102
103                 CDEBUG(D_CONFIG, "connect to %s(%s)\n",
104                        mdc->obd_name, mdc->obd_uuid.uuid);
105
106                 OBD_ALLOC_PTR(ocd);
107                 if (!ocd)
108                         RETURN(-ENOMEM);
109                 /* The connection between MDS must be local */
110                 ocd->ocd_connect_flags = OBD_CONNECT_LCL_CLIENT | 
111                                          OBD_CONNECT_MDS_CAPA |
112                                          OBD_CONNECT_OSS_CAPA;
113                 rc = obd_connect(env, conn, mdc, &mdc->obd_uuid, ocd);
114                 OBD_FREE_PTR(ocd);
115                 if (rc) {
116                         CERROR("target %s connect error %d\n",
117                                mdc->obd_name, rc);
118                 } else {
119                         desc->cl_exp = class_conn2export(conn);
120
121                         rc = obd_fid_init(desc->cl_exp);
122                         if (rc)
123                                 CERROR("fid init error %d \n", rc);
124                         else {
125                                 /* obd notify mechanism */
126                                 mdc->obd_upcall.onu_owner = mc;
127                                 mdc->obd_upcall.onu_upcall = mdc_obd_update;
128                         }
129                 }
130                 if (rc) {
131                         obd_disconnect(desc->cl_exp);
132                         desc->cl_exp = NULL;
133                 }
134         }
135
136         RETURN(rc);
137 }
138
139 static int mdc_obd_del(const struct lu_env *env, struct mdc_device *mc,
140                        struct lustre_cfg *cfg)
141 {
142         struct mdc_cli_desc *desc = &mc->mc_desc;
143         const char *dev = lustre_cfg_string(cfg, 0);
144         struct obd_device *mdc_obd = class_exp2obd(desc->cl_exp);
145         struct obd_device *mdt_obd;
146         int rc;
147
148         ENTRY;
149
150         CDEBUG(D_CONFIG, "Disconnect from %s\n",
151                mdc_obd->obd_name);
152
153         /* Set mdt_obd flags in shutdown. */
154         mdt_obd = class_name2obd(dev);
155         LASSERT(mdt_obd != NULL);
156         if (mdc_obd) {
157                 mdc_obd->obd_no_recov = mdt_obd->obd_no_recov;
158                 mdc_obd->obd_force = mdt_obd->obd_force;
159                 mdc_obd->obd_fail = 0;
160         }
161
162         rc = obd_fid_fini(desc->cl_exp);
163         if (rc)
164                 CERROR("Fid fini error %d\n", rc);
165
166         obd_register_observer(mdc_obd, NULL);
167         rc = obd_disconnect(desc->cl_exp);
168         if (rc) {
169                 CERROR("Target %s disconnect error %d\n",
170                        mdc_obd->obd_name, rc);
171         }
172         class_manual_cleanup(mdc_obd);
173         desc->cl_exp = NULL;
174
175         RETURN(rc);
176 }
177
178 static int mdc_process_config(const struct lu_env *env,
179                               struct lu_device *ld,
180                               struct lustre_cfg *cfg)
181 {
182         struct mdc_device *mc = lu2mdc_dev(ld);
183         int rc;
184
185         ENTRY;
186         switch (cfg->lcfg_command) {
187         case LCFG_ADD_MDC:
188                 rc = mdc_obd_add(env, mc, cfg);
189                 break;
190         case LCFG_CLEANUP:
191                 rc = mdc_obd_del(env, mc, cfg);
192                 break;
193         default:
194                 rc = -EOPNOTSUPP;
195         }
196         RETURN(rc);
197 }
198
199 static struct lu_device_operations mdc_lu_ops = {
200         .ldo_object_alloc   = mdc_object_alloc,
201         .ldo_process_config = mdc_process_config
202 };
203
204 void mdc_init_ea_size(const struct lu_env *env, struct mdc_device *mc, 
205                       int max_mdsize, int max_cookiesize)
206 {
207         struct obd_device *obd = class_exp2obd(mc->mc_desc.cl_exp);
208        
209         obd->u.cli.cl_max_mds_easize = max_mdsize;
210         obd->u.cli.cl_max_mds_cookiesize = max_cookiesize;
211 }
212
213 static int mdc_device_init(const struct lu_env *env, struct lu_device *ld, 
214                            const char *name, struct lu_device *next)
215 {
216         return 0;
217 }
218
219 static struct lu_device *mdc_device_fini(const struct lu_env *env,
220                                          struct lu_device *ld)
221 {
222         ENTRY;
223         RETURN (NULL);
224 }
225
226 struct lu_device *mdc_device_alloc(const struct lu_env *env,
227                                    struct lu_device_type *ldt,
228                                    struct lustre_cfg *cfg)
229 {
230         struct lu_device  *ld;
231         struct mdc_device *mc;
232         ENTRY;
233
234         OBD_ALLOC_PTR(mc);
235         if (mc == NULL) {
236                 ld = ERR_PTR(-ENOMEM);
237         } else {
238                 md_device_init(&mc->mc_md_dev, ldt);
239                 mc->mc_md_dev.md_ops = &mdc_md_ops;
240                 ld = mdc2lu_dev(mc);
241                 ld->ld_ops = &mdc_lu_ops;
242                 sema_init(&mc->mc_fid_sem, 1);
243
244         }
245
246         RETURN (ld);
247 }
248 void mdc_device_free(const struct lu_env *env, struct lu_device *ld)
249 {
250         struct mdc_device *mc = lu2mdc_dev(ld);
251
252         LASSERTF(atomic_read(&ld->ld_ref) == 0,
253                  "Refcount = %i\n", atomic_read(&ld->ld_ref));
254         LASSERT(list_empty(&mc->mc_linkage));
255         md_device_fini(&mc->mc_md_dev);
256         OBD_FREE_PTR(mc);
257 }
258
259 /* context key constructor/destructor */
260
261 static void *mdc_key_init(const struct lu_context *ctx,
262                           struct lu_context_key *key)
263 {
264         struct mdc_thread_info *info;
265
266         CLASSERT(CFS_PAGE_SIZE >= sizeof *info);
267         OBD_ALLOC_PTR(info);
268         if (info == NULL)
269                 info = ERR_PTR(-ENOMEM);
270         return info;
271 }
272
273 static void mdc_key_fini(const struct lu_context *ctx,
274                          struct lu_context_key *key, void *data)
275 {
276         struct mdc_thread_info *info = data;
277         OBD_FREE_PTR(info);
278 }
279
280 struct lu_context_key mdc_thread_key = {
281         .lct_tags = LCT_MD_THREAD|LCT_CL_THREAD,
282         .lct_init = mdc_key_init,
283         .lct_fini = mdc_key_fini
284 };
285
286 int mdc_type_init(struct lu_device_type *ldt)
287 {
288         return lu_context_key_register(&mdc_thread_key);
289 }
290
291 void mdc_type_fini(struct lu_device_type *ldt)
292 {
293         lu_context_key_degister(&mdc_thread_key);
294 }
295
296 static struct lu_device_type_operations mdc_device_type_ops = {
297         .ldto_init = mdc_type_init,
298         .ldto_fini = mdc_type_fini,
299
300         .ldto_device_alloc = mdc_device_alloc,
301         .ldto_device_free  = mdc_device_free,
302
303         .ldto_device_init = mdc_device_init,
304         .ldto_device_fini = mdc_device_fini
305 };
306
307 struct lu_device_type mdc_device_type = {
308         .ldt_tags     = LU_DEVICE_MD,
309         .ldt_name     = LUSTRE_CMM_MDC_NAME,
310         .ldt_ops      = &mdc_device_type_ops,
311         .ldt_ctx_tags = LCT_MD_THREAD|LCT_CL_THREAD
312 };
313