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