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