Whamcloud - gitweb
LU-1347 style: removes obsolete EXPORT_SYMTAB macros
[fs/lustre-release.git] / lustre / cmm / mdc_device.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Whamcloud, Inc.
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 #define DEBUG_SUBSYSTEM S_MDS
44
45 #include <obd.h>
46 #include <obd_class.h>
47 #include <lprocfs_status.h>
48 #include <lustre_ver.h>
49 #include "cmm_internal.h"
50 #include "mdc_internal.h"
51
52 static const struct lu_device_operations mdc_lu_ops;
53 /**
54  * \addtogroup cmm_mdc
55  * @{
56  */
57 /**
58  * The md_device_operation for mdc. It is empty.
59  */
60 static const struct md_device_operations mdc_md_ops = { 0 };
61
62 /**
63  * Upcall handler in mdc. Analog of obd_device::o_notify().
64  */
65 static int mdc_obd_update(struct obd_device *host,
66                           struct obd_device *watched,
67                           enum obd_notify_event ev, void *owner, void *data)
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 /**
96  * Add new mdc device.
97  * Invoked by configuration command LCFG_ADD_MDC.
98  *
99  * MDC OBD is set up already and connected to the proper MDS
100  * mdc_add_obd() find that obd by uuid and connects to it.
101  * Local MDT uuid is used for connection.
102  */
103 static int mdc_obd_add(const struct lu_env *env,
104                        struct mdc_device *mc, struct lustre_cfg *cfg)
105 {
106         struct mdc_cli_desc *desc = &mc->mc_desc;
107         struct obd_device *mdc;
108         const char *uuid_str = lustre_cfg_string(cfg, 1);
109         const char *index = lustre_cfg_string(cfg, 2);
110         const char *mdc_uuid_str = lustre_cfg_string(cfg, 4);
111         struct md_site *ms = lu_site2md(mdc2lu_dev(mc)->ld_site);
112         char *p;
113         int rc = 0;
114
115         ENTRY;
116         LASSERT(uuid_str);
117         LASSERT(index);
118
119         mc->mc_num = simple_strtol(index, &p, 10);
120         if (*p) {
121                 CERROR("Invalid index in lustre_cgf, offset 2\n");
122                 RETURN(-EINVAL);
123         }
124
125         obd_str2uuid(&desc->cl_srv_uuid, uuid_str);
126         obd_str2uuid(&desc->cl_cli_uuid, mdc_uuid_str);
127         /* try to find MDC OBD connected to the needed MDT */
128         mdc = class_find_client_obd(&desc->cl_srv_uuid, LUSTRE_MDC_NAME,
129                                     &desc->cl_cli_uuid);
130         if (!mdc) {
131                 CERROR("Cannot find MDC OBD connected to %s\n", uuid_str);
132                 rc = -ENOENT;
133         } else if (!mdc->obd_set_up) {
134                 CERROR("target %s not set up\n", mdc->obd_name);
135                 rc = -EINVAL;
136         } else {
137                 struct obd_connect_data *ocd;
138
139                 CDEBUG(D_CONFIG, "connect to %s(%s)\n",
140                        mdc->obd_name, mdc->obd_uuid.uuid);
141
142                 OBD_ALLOC_PTR(ocd);
143                 if (!ocd)
144                         RETURN(-ENOMEM);
145                 /*
146                  * The connection between MDS must be local,
147                  * IBITS are needed for rename_lock (INODELOCK_UPDATE)
148                  */
149                 ocd->ocd_ibits_known = MDS_INODELOCK_UPDATE;
150                 ocd->ocd_connect_flags = OBD_CONNECT_VERSION |
151                                          OBD_CONNECT_ACL |
152                                          OBD_CONNECT_RMT_CLIENT |
153                                          OBD_CONNECT_MDS_CAPA |
154                                          OBD_CONNECT_OSS_CAPA |
155                                          OBD_CONNECT_IBITS |
156                                          OBD_CONNECT_BRW_SIZE |
157                                          OBD_CONNECT_MDS_MDS |
158                                          OBD_CONNECT_FID |
159                                          OBD_CONNECT_AT |
160                                          OBD_CONNECT_FULL20 |
161                                          OBD_CONNECT_64BITHASH;
162                 ocd->ocd_brw_size = PTLRPC_MAX_BRW_SIZE;
163                 rc = obd_connect(env, &desc->cl_exp, mdc, &mdc->obd_uuid, ocd, NULL);
164                 OBD_FREE_PTR(ocd);
165                 if (rc) {
166                         CERROR("target %s connect error %d\n",
167                                mdc->obd_name, rc);
168                 } else {
169                         /* set seq controller export for MDC0 if exists */
170                         if (mc->mc_num == 0)
171                                 ms->ms_control_exp =
172                                         class_export_get(desc->cl_exp);
173                         rc = obd_fid_init(desc->cl_exp);
174                         if (rc)
175                                 CERROR("fid init error %d \n", rc);
176                         else {
177                                 /* obd notify mechanism */
178                                 mdc->obd_upcall.onu_owner = mc;
179                                 mdc->obd_upcall.onu_upcall = mdc_obd_update;
180                         }
181                 }
182
183                 if (rc) {
184                         obd_disconnect(desc->cl_exp);
185                         desc->cl_exp = NULL;
186                 }
187         }
188
189         RETURN(rc);
190 }
191
192 /**
193  * Delete mdc device.
194  * Called when configuration command LCFG_CLEANUP is issued.
195  *
196  * This disconnects MDC OBD and cleanup it.
197  */
198 static int mdc_obd_del(const struct lu_env *env, struct mdc_device *mc,
199                        struct lustre_cfg *cfg)
200 {
201         struct mdc_cli_desc *desc = &mc->mc_desc;
202         const char *dev = lustre_cfg_string(cfg, 0);
203         struct obd_device *mdc_obd = class_exp2obd(desc->cl_exp);
204         struct obd_device *mdt_obd;
205         int rc;
206
207         ENTRY;
208
209         CDEBUG(D_CONFIG, "Disconnect from %s\n",
210                mdc_obd->obd_name);
211
212         /* Set mdt_obd flags in shutdown. */
213         mdt_obd = class_name2obd(dev);
214         LASSERT(mdt_obd != NULL);
215         if (mdc_obd) {
216                 mdc_obd->obd_no_recov = mdt_obd->obd_no_recov;
217                 mdc_obd->obd_force = mdt_obd->obd_force;
218                 mdc_obd->obd_fail = 0;
219         }
220
221         rc = obd_fid_fini(desc->cl_exp);
222         if (rc)
223                 CERROR("Fid fini error %d\n", rc);
224
225         obd_register_observer(mdc_obd, NULL);
226         mdc_obd->obd_upcall.onu_owner = NULL;
227         mdc_obd->obd_upcall.onu_upcall = NULL;
228         rc = obd_disconnect(desc->cl_exp);
229         if (rc) {
230                 CERROR("Target %s disconnect error %d\n",
231                        mdc_obd->obd_name, rc);
232         }
233         class_manual_cleanup(mdc_obd);
234         desc->cl_exp = NULL;
235
236         RETURN(0);
237 }
238
239 /**
240  * Process config command. Passed to the mdc from mdt.
241  * Supports two commands only - LCFG_ADD_MDC and LCFG_CLEANUP
242  */
243 static int mdc_process_config(const struct lu_env *env,
244                               struct lu_device *ld,
245                               struct lustre_cfg *cfg)
246 {
247         struct mdc_device *mc = lu2mdc_dev(ld);
248         int rc;
249
250         ENTRY;
251         switch (cfg->lcfg_command) {
252         case LCFG_ADD_MDC:
253                 rc = mdc_obd_add(env, mc, cfg);
254                 break;
255         case LCFG_CLEANUP:
256                 rc = mdc_obd_del(env, mc, cfg);
257                 break;
258         default:
259                 rc = -EOPNOTSUPP;
260         }
261         RETURN(rc);
262 }
263
264 /**
265  * lu_device_operations instance for mdc.
266  */
267 static const struct lu_device_operations mdc_lu_ops = {
268         .ldo_object_alloc   = mdc_object_alloc,
269         .ldo_process_config = mdc_process_config
270 };
271
272 /**
273  * Initialize proper easize and cookie size.
274  */
275 void cmm_mdc_init_ea_size(const struct lu_env *env, struct mdc_device *mc,
276                       int max_mdsize, int max_cookiesize)
277 {
278         struct obd_device *obd = class_exp2obd(mc->mc_desc.cl_exp);
279
280         obd->u.cli.cl_max_mds_easize = max_mdsize;
281         obd->u.cli.cl_max_mds_cookiesize = max_cookiesize;
282 }
283
284 /** Start mdc device */
285 static int mdc_device_init(const struct lu_env *env, struct lu_device *ld,
286                            const char *name, struct lu_device *next)
287 {
288         return 0;
289 }
290
291 /** Stop mdc device. */
292 static struct lu_device *mdc_device_fini(const struct lu_env *env,
293                                          struct lu_device *ld)
294 {
295         ENTRY;
296         RETURN (NULL);
297 }
298
299 /** Allocate new mdc device */
300 static struct lu_device *mdc_device_alloc(const struct lu_env *env,
301                                           struct lu_device_type *ldt,
302                                           struct lustre_cfg *cfg)
303 {
304         struct lu_device  *ld;
305         struct mdc_device *mc;
306         ENTRY;
307
308         OBD_ALLOC_PTR(mc);
309         if (mc == NULL) {
310                 ld = ERR_PTR(-ENOMEM);
311         } else {
312                 md_device_init(&mc->mc_md_dev, ldt);
313                 mc->mc_md_dev.md_ops = &mdc_md_ops;
314                 ld = mdc2lu_dev(mc);
315                 ld->ld_ops = &mdc_lu_ops;
316                 cfs_sema_init(&mc->mc_fid_sem, 1);
317         }
318
319         RETURN (ld);
320 }
321
322 /** Free mdc device */
323 static struct lu_device *mdc_device_free(const struct lu_env *env,
324                                          struct lu_device *ld)
325 {
326         struct mdc_device *mc = lu2mdc_dev(ld);
327
328         LASSERTF(cfs_atomic_read(&ld->ld_ref) == 0,
329                  "Refcount = %d\n", cfs_atomic_read(&ld->ld_ref));
330         LASSERT(cfs_list_empty(&mc->mc_linkage));
331         md_device_fini(&mc->mc_md_dev);
332         OBD_FREE_PTR(mc);
333         return NULL;
334 }
335
336 /** context key constructor/destructor: mdc_key_init, mdc_key_fini */
337 LU_KEY_INIT_FINI(mdc, struct mdc_thread_info);
338
339 /** context key: mdc_thread_key */
340 LU_CONTEXT_KEY_DEFINE(mdc, LCT_MD_THREAD|LCT_CL_THREAD);
341
342 /** type constructor/destructor: mdc_type_init, mdc_type_fini */
343 LU_TYPE_INIT_FINI(mdc, &mdc_thread_key);
344
345 static struct lu_device_type_operations mdc_device_type_ops = {
346         .ldto_init = mdc_type_init,
347         .ldto_fini = mdc_type_fini,
348
349         .ldto_start = mdc_type_start,
350         .ldto_stop  = mdc_type_stop,
351
352         .ldto_device_alloc = mdc_device_alloc,
353         .ldto_device_free  = mdc_device_free,
354
355         .ldto_device_init = mdc_device_init,
356         .ldto_device_fini = mdc_device_fini
357 };
358
359 struct lu_device_type mdc_device_type = {
360         .ldt_tags     = LU_DEVICE_MD,
361         .ldt_name     = LUSTRE_CMM_MDC_NAME,
362         .ldt_ops      = &mdc_device_type_ops,
363         .ldt_ctx_tags = LCT_MD_THREAD|LCT_CL_THREAD
364 };
365 /** @} */