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