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