1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Lustre Metadata Client (mdc)
7 * Copyright (c) 2006 Cluster File Systems, Inc.
8 * Author: Mike Pershin <tappro@clusterfs.com>
10 * This file is part of the Lustre file system, http://www.lustre.org
11 * Lustre is a trademark of Cluster File Systems, Inc.
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.
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.
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.
30 # define EXPORT_SYMTAB
32 #define DEBUG_SUBSYSTEM S_MDS
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"
41 static struct lu_device_operations mdc_lu_ops;
43 static inline int lu_device_is_mdc(struct lu_device *ld)
45 return ergo(ld != NULL && ld->ld_ops != NULL,
46 ld->ld_ops == &mdc_lu_ops);
49 static struct md_device_operations mdc_md_ops = { 0 };
51 static int mdc_obd_update(struct obd_device *host,
52 struct obd_device *watched,
53 enum obd_notify_event ev, void *owner)
55 struct mdc_device *mc = owner;
60 CDEBUG(D_CONFIG, "notify %s ev=%d\n", watched->obd_name, ev);
63 /* MDC OBD is set up already and connected to the proper MDS
64 * mdc_add_obd() find that obd by uuid and connects to it.
65 * Local MDT uuid is used for connection
67 static int mdc_add_obd(const struct lu_context *ctx,
68 struct mdc_device *mc, struct lustre_cfg *cfg)
70 struct mdc_cli_desc *desc = &mc->mc_desc;
71 struct obd_device *mdc;
72 const char *uuid_str = lustre_cfg_string(cfg, 1);
73 const char *index = lustre_cfg_string(cfg, 2);
74 const char *mdc_uuid_str = lustre_cfg_string(cfg, 4);
82 mc->mc_num = simple_strtol(index, &p, 10);
84 CERROR("Invalid index in lustre_cgf, offset 2\n");
88 obd_str2uuid(&desc->cl_srv_uuid, uuid_str);
89 obd_str2uuid(&desc->cl_cli_uuid, mdc_uuid_str);
90 /* try to find MDC OBD connected to the needed MDT */
91 mdc = class_find_client_obd(&desc->cl_srv_uuid, LUSTRE_MDC_NAME,
94 CERROR("Cannot find MDC OBD connected to %s\n", uuid_str);
96 } else if (!mdc->obd_set_up) {
97 CERROR("target %s not set up\n", mdc->obd_name);
100 struct lustre_handle *conn = &desc->cl_conn;
101 struct obd_connect_data *ocd;
103 CDEBUG(D_CONFIG, "connect to %s(%s)\n",
104 mdc->obd_name, mdc->obd_uuid.uuid);
109 /* The connection between MDS must be local */
110 ocd->ocd_connect_flags |= OBD_CONNECT_LCL_CLIENT;
111 rc = obd_connect(ctx, conn, mdc, &mdc->obd_uuid, ocd);
114 CERROR("target %s connect error %d\n",
117 desc->cl_exp = class_conn2export(conn);
119 rc = obd_fid_init(desc->cl_exp);
121 CERROR("fid init error %d \n", rc);
123 /* obd notify mechanism */
124 mdc->obd_upcall.onu_owner = mc;
125 mdc->obd_upcall.onu_upcall = mdc_obd_update;
129 obd_disconnect(desc->cl_exp);
137 static int mdc_del_obd(struct mdc_device *mc)
139 struct mdc_cli_desc *desc = &mc->mc_desc;
140 struct obd_device *mdc_obd = class_exp2obd(desc->cl_exp);
145 CDEBUG(D_CONFIG, "disconnect from %s\n",
148 rc = obd_fid_fini(desc->cl_exp);
150 CERROR("fid init error %d \n", rc);
152 obd_register_observer(mdc_obd, NULL);
154 /*TODO: Give the same shutdown flags as we have */
156 desc->cl_exp->exp_obd->obd_force = mdt_obd->obd_force;
157 desc->cl_exp->exp_obd->obd_fail = mdt_obd->obd_fail;
159 rc = obd_disconnect(desc->cl_exp);
161 CERROR("target %s disconnect error %d\n",
162 mdc_obd->obd_name, rc);
164 class_manual_cleanup(mdc_obd);
170 static int mdc_process_config(const struct lu_context *ctx,
171 struct lu_device *ld, struct lustre_cfg *cfg)
173 struct mdc_device *mc = lu2mdc_dev(ld);
177 switch (cfg->lcfg_command) {
179 rc = mdc_add_obd(ctx, mc, cfg);
187 static struct lu_device_operations mdc_lu_ops = {
188 .ldo_object_alloc = mdc_object_alloc,
189 .ldo_process_config = mdc_process_config
192 static int mdc_device_init(const struct lu_context *ctx,
193 struct lu_device *ld, struct lu_device *next)
198 static struct lu_device *mdc_device_fini(const struct lu_context *ctx,
199 struct lu_device *ld)
201 struct mdc_device *mc = lu2mdc_dev(ld);
210 struct lu_device *mdc_device_alloc(const struct lu_context *ctx,
211 struct lu_device_type *ldt,
212 struct lustre_cfg *cfg)
214 struct lu_device *ld;
215 struct mdc_device *mc;
221 ld = ERR_PTR(-ENOMEM);
223 md_device_init(&mc->mc_md_dev, ldt);
224 mc->mc_md_dev.md_ops = &mdc_md_ops;
226 ld->ld_ops = &mdc_lu_ops;
231 void mdc_device_free(const struct lu_context *ctx, struct lu_device *ld)
233 struct mdc_device *mc = lu2mdc_dev(ld);
235 LASSERT(atomic_read(&ld->ld_ref) == 0);
236 LASSERT(list_empty(&mc->mc_linkage));
237 md_device_fini(&mc->mc_md_dev);
241 /* context key constructor/destructor */
243 static void *mdc_thread_init(const struct lu_context *ctx,
244 struct lu_context_key *key)
246 struct mdc_thread_info *info;
248 CLASSERT(CFS_PAGE_SIZE >= sizeof *info);
251 info = ERR_PTR(-ENOMEM);
255 static void mdc_thread_fini(const struct lu_context *ctx,
256 struct lu_context_key *key, void *data)
258 struct mdc_thread_info *info = data;
262 struct lu_context_key mdc_thread_key = {
263 .lct_tags = LCT_MD_THREAD|LCT_CL_THREAD,
264 .lct_init = mdc_thread_init,
265 .lct_fini = mdc_thread_fini
268 int mdc_type_init(struct lu_device_type *ldt)
270 return lu_context_key_register(&mdc_thread_key);
273 void mdc_type_fini(struct lu_device_type *ldt)
275 lu_context_key_degister(&mdc_thread_key);
278 static struct lu_device_type_operations mdc_device_type_ops = {
279 .ldto_init = mdc_type_init,
280 .ldto_fini = mdc_type_fini,
282 .ldto_device_alloc = mdc_device_alloc,
283 .ldto_device_free = mdc_device_free,
285 .ldto_device_init = mdc_device_init,
286 .ldto_device_fini = mdc_device_fini
289 struct lu_device_type mdc_device_type = {
290 .ldt_tags = LU_DEVICE_MD,
291 .ldt_name = LUSTRE_CMM_MDC_NAME,
292 .ldt_ops = &mdc_device_type_ops,
293 .ldt_ctx_tags = LCT_MD_THREAD|LCT_CL_THREAD