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