4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 021110-1307, USA
24 * Copyright (c) 2012, 2017, Intel Corporation.
25 * Use is subject to license terms.
27 * Author: Johann Lombardi <johann.lombardi@intel.com>
28 * Author: Niu Yawei <yawei.niu@intel.com>
32 * Management of the device associated with a Quota Master Target (QMT).
34 * The QMT holds the cluster wide quota limits. It stores the quota settings
35 * ({hard,soft} limit & grace time) in a global index file and is in charge
36 * of allocating quota space to slaves while guaranteeing that the overall
37 * limits aren't exceeded. The QMT also maintains one index per slave (in fact,
38 * one per slave per quota type) used to track how much space is allocated
39 * to a given slave. Now that the QMT is aware of the quota space distribution
40 * among slaves, it can afford to rebalance efficiently quota space from one
41 * slave to another. Slaves are asked to release quota space via glimpse
42 * callbacks sent on DLM locks which are granted to slaves when those latters
43 * acquire quota space.
45 * The QMT device is currently set up by the MDT and should probably be moved
46 * to a separate target in the future. Meanwhile, the MDT forwards all quota
47 * requests to the QMT via a list of request handlers (see struct qmt_handlers
48 * in lustre_quota.h). The QMT also borrows the LDLM namespace from the MDT.
50 * To bring up a QMT device, the following steps must be completed:
52 * - call ->ldto_device_alloc to allocate the QMT device and perform basic
53 * initialization like connecting to the backend OSD device or setting up the
54 * default pools and the QMT procfs directory.
56 * - the MDT can then connect to the QMT instance via legacy obd_connect path.
58 * - once the MDT stack has been fully configured, ->ldto_prepare must be called
59 * to configure on-disk objects associated with this master target.
61 * To shutdown a QMT device, the MDT just has to disconnect from the QMT.
63 * The qmt_device_type structure is registered when the lquota module is
64 * loaded and all the steps described above are automatically done when the MDT
65 * set up the Quota Master Target via calls to class_attach/class_setup, see
66 * mdt_quota_init() for more details.
69 #define DEBUG_SUBSYSTEM S_LQUOTA
71 #include <obd_class.h>
72 #include <lprocfs_status.h>
73 #include <lustre_disk.h>
74 #include "qmt_internal.h"
76 static const struct lu_device_operations qmt_lu_ops;
79 * Release quota master target and all data structure associated with this
81 * Called on MDT0 cleanup.
83 * \param env - is the environment passed by the caller
84 * \param ld - is the lu_device associated with the qmt device to be released
86 * \retval - NULL on success (backend OSD device is managed by the main stack),
87 * appropriate error on failure
89 static struct lu_device *qmt_device_fini(const struct lu_env *env,
92 struct qmt_device *qmt = lu2qmt_dev(ld);
97 CDEBUG(D_QUOTA, "%s: initiating QMT shutdown\n", qmt->qmt_svname);
98 qmt->qmt_stopping = true;
100 /* kill pool instances, if any */
101 qmt_pool_fini(env, qmt);
103 /* remove qmt proc entry */
104 if (qmt->qmt_proc != NULL && !IS_ERR(qmt->qmt_proc)) {
105 lprocfs_remove(&qmt->qmt_proc);
106 qmt->qmt_proc = NULL;
109 /* stop rebalance thread */
110 if (!qmt->qmt_child->dd_rdonly)
111 qmt_stop_reba_thread(qmt);
113 /* disconnect from OSD */
114 if (qmt->qmt_child_exp != NULL) {
115 obd_disconnect(qmt->qmt_child_exp);
116 qmt->qmt_child_exp = NULL;
117 qmt->qmt_child = NULL;
120 /* clear references to MDT namespace */
121 ld->ld_obd->obd_namespace = NULL;
128 * Connect a quota master to the backend OSD device.
130 * \param env - is the environment passed by the caller
131 * \param qmt - is the quota master target to be connected
132 * \param cfg - is the configuration log record from which we need to extract
133 * the service name of the backend OSD device to connect to.
135 * \retval - 0 on success, appropriate error on failure
137 static int qmt_connect_to_osd(const struct lu_env *env, struct qmt_device *qmt,
138 struct lustre_cfg *cfg)
140 struct obd_connect_data *data = NULL;
141 struct obd_device *obd;
142 struct lu_device *ld = qmt2lu_dev(qmt);
146 LASSERT(qmt->qmt_child_exp == NULL);
150 GOTO(out, rc = -ENOMEM);
152 /* look-up OBD device associated with the backend OSD device.
153 * The MDT is kind enough to pass the OBD name in QMT configuration */
154 obd = class_name2obd(lustre_cfg_string(cfg, 3));
156 CERROR("%s: can't locate backend osd device: %s\n",
157 qmt->qmt_svname, lustre_cfg_string(cfg, 3));
158 GOTO(out, rc = -ENOTCONN);
161 data->ocd_connect_flags = OBD_CONNECT_VERSION;
162 data->ocd_version = LUSTRE_VERSION_CODE;
164 /* connect to OSD device */
165 rc = obd_connect(NULL, &qmt->qmt_child_exp, obd, &obd->obd_uuid, data,
168 CERROR("%s: cannot connect to osd dev %s (%d)\n",
169 qmt->qmt_svname, obd->obd_name, rc);
173 /* initialize site (although it isn't used anywhere) and lu_device
174 * pointer to next device */
175 qmt->qmt_child = lu2dt_dev(qmt->qmt_child_exp->exp_obd->obd_lu_dev);
176 ld->ld_site = qmt->qmt_child_exp->exp_obd->obd_lu_dev->ld_site;
185 * Initialize quota master target device. This includers connecting to
186 * the backend OSD device, initializing the pool configuration and creating the
187 * root procfs directory dedicated to this quota target.
188 * The rest of the initialization is done when the stack is fully configured
189 * (i.e. when ->ldo_start is called across the stack).
191 * This function is called on MDT0 setup.
193 * \param env - is the environment passed by the caller
194 * \param qmt - is the quota master target to be initialized
195 * \param ldt - is the device type structure associated with the qmt device
196 * \param cfg - is the configuration record used to configure the qmt device
198 * \retval - 0 on success, appropriate error on failure
200 static int qmt_device_init0(const struct lu_env *env, struct qmt_device *qmt,
201 struct lu_device_type *ldt, struct lustre_cfg *cfg)
203 struct lu_device *ld = qmt2lu_dev(qmt);
204 struct obd_device *obd, *mdt_obd;
205 struct obd_type *type;
206 char *svname = lustre_cfg_string(cfg, 0);
213 /* record who i am, it might be useful ... */
214 rc = strlcpy(qmt->qmt_svname, svname, sizeof(qmt->qmt_svname));
215 if (rc >= sizeof(qmt->qmt_svname))
218 /* look-up the obd_device associated with the qmt */
219 obd = class_name2obd(qmt->qmt_svname);
223 /* reference each other */
224 obd->obd_lu_dev = ld;
227 /* look-up the parent MDT to steal its ldlm namespace ... */
228 mdt_obd = class_name2obd(lustre_cfg_string(cfg, 2));
232 /* borrow MDT namespace. kind of a hack until we have our own namespace
233 * & service threads */
234 LASSERT(mdt_obd->obd_namespace != NULL);
235 obd->obd_namespace = mdt_obd->obd_namespace;
236 qmt->qmt_ns = obd->obd_namespace;
238 /* connect to backend osd device */
239 rc = qmt_connect_to_osd(env, qmt, cfg);
243 /* set up and start rebalance thread */
244 thread_set_flags(&qmt->qmt_reba_thread, SVC_STARTING);
245 init_waitqueue_head(&qmt->qmt_reba_thread.t_ctl_waitq);
246 INIT_LIST_HEAD(&qmt->qmt_reba_list);
247 spin_lock_init(&qmt->qmt_reba_lock);
248 if (!qmt->qmt_child->dd_rdonly) {
249 rc = qmt_start_reba_thread(qmt);
251 CERROR("%s: failed to start rebalance thread (%d)\n",
252 qmt->qmt_svname, rc);
257 /* at the moment there is no linkage between lu_type and obd_type, so
258 * we lookup obd_type this way */
259 type = class_search_type(LUSTRE_QMT_NAME);
260 LASSERT(type != NULL);
262 /* put reference taken by class_search_type */
263 kobject_put(&type->typ_kobj);
265 /* register proc directory associated with this qmt */
266 qmt->qmt_proc = lprocfs_register(qmt->qmt_svname, type->typ_procroot,
268 if (IS_ERR(qmt->qmt_proc)) {
269 rc = PTR_ERR(qmt->qmt_proc);
270 CERROR("%s: failed to create qmt proc entry (%d)\n",
271 qmt->qmt_svname, rc);
275 /* initialize pool configuration */
276 rc = qmt_pool_init(env, qmt);
282 qmt_device_fini(env, ld);
287 * Free quota master target device. Companion of qmt_device_alloc()
289 * \param env - is the environment passed by the caller
290 * \param ld - is the lu_device associated with the qmt dev to be freed
292 * \retval - NULL on success (backend OSD device is managed by the main stack),
293 * appropriate error on failure
295 static struct lu_device *qmt_device_free(const struct lu_env *env,
296 struct lu_device *ld)
298 struct qmt_device *qmt = lu2qmt_dev(ld);
301 LASSERT(qmt != NULL);
309 * Allocate quota master target and initialize it.
311 * \param env - is the environment passed by the caller
312 * \param ldt - is the device type structure associated with the qmt
313 * \param cfg - is the configuration record used to configure the qmt
315 * \retval - lu_device structure associated with the qmt on success,
316 * appropriate error on failure
318 static struct lu_device *qmt_device_alloc(const struct lu_env *env,
319 struct lu_device_type *ldt,
320 struct lustre_cfg *cfg)
322 struct qmt_device *qmt;
323 struct lu_device *ld;
327 /* allocate qmt device */
330 RETURN(ERR_PTR(-ENOMEM));
332 /* configure lu/dt_device */
333 ld = qmt2lu_dev(qmt);
334 dt_device_init(&qmt->qmt_dt_dev, ldt);
335 ld->ld_ops = &qmt_lu_ops;
337 /* initialize qmt device */
338 rc = qmt_device_init0(env, qmt, ldt, cfg);
340 qmt_device_free(env, ld);
347 LU_KEY_INIT_FINI(qmt, struct qmt_thread_info);
348 LU_TYPE_INIT_FINI(qmt, &qmt_thread_key);
349 LU_CONTEXT_KEY_DEFINE(qmt, LCT_MD_THREAD);
352 * lu device type operations associated with the master target.
354 static struct lu_device_type_operations qmt_device_type_ops = {
355 .ldto_init = qmt_type_init,
356 .ldto_fini = qmt_type_fini,
358 .ldto_start = qmt_type_start,
359 .ldto_stop = qmt_type_stop,
361 .ldto_device_alloc = qmt_device_alloc,
362 .ldto_device_free = qmt_device_free,
364 .ldto_device_fini = qmt_device_fini,
368 * lu device type structure associated with the master target.
369 * MDT0 uses this structure to configure the qmt.
371 static struct lu_device_type qmt_device_type = {
372 .ldt_tags = LU_DEVICE_DT,
373 .ldt_name = LUSTRE_QMT_NAME,
374 .ldt_ops = &qmt_device_type_ops,
375 .ldt_ctx_tags = LCT_MD_THREAD,
379 * obd_connect handler used by the MDT to connect to the master target.
381 static int qmt_device_obd_connect(const struct lu_env *env,
382 struct obd_export **exp,
383 struct obd_device *obd,
384 struct obd_uuid *cluuid,
385 struct obd_connect_data *data,
388 struct lustre_handle conn;
392 rc = class_connect(&conn, obd, cluuid);
396 *exp = class_conn2export(&conn);
401 * obd_disconnect handler used by the MDT to disconnect from the master target.
402 * We trigger cleanup on disconnect since it means that the MDT is about to
405 static int qmt_device_obd_disconnect(struct obd_export *exp)
407 struct obd_device *obd = exp->exp_obd;
411 rc = class_disconnect(exp);
415 rc = class_manual_cleanup(obd);
420 * obd device operations associated with the master target.
422 static const struct obd_ops qmt_obd_ops = {
423 .o_owner = THIS_MODULE,
424 .o_connect = qmt_device_obd_connect,
425 .o_disconnect = qmt_device_obd_disconnect,
429 * Called when the MDS is fully configured. We use it to set up local objects
430 * associated with the quota master target.
432 * \param env - is the environment passed by the caller
433 * \param parent - is the lu_device of the parent, that's to say the mdt
434 * \param ld - is the lu_device associated with the master target
436 * \retval - 0 on success, appropriate error on failure
438 static int qmt_device_prepare(const struct lu_env *env,
439 struct lu_device *parent,
440 struct lu_device *ld)
442 struct qmt_device *qmt = lu2qmt_dev(ld);
443 struct dt_object *qmt_root;
447 /* initialize quota master root directory where all index files will be
449 qmt_root = lquota_disk_dir_find_create(env, qmt->qmt_child, NULL,
451 if (IS_ERR(qmt_root)) {
452 rc = PTR_ERR(qmt_root);
453 CERROR("%s: failed to create master quota directory (%d)\n",
454 qmt->qmt_svname, rc);
458 /* initialize on-disk indexes associated with each pool */
459 rc = qmt_pool_prepare(env, qmt, qmt_root);
461 dt_object_put(env, qmt_root);
466 * lu device operations for the quota master target
468 static const struct lu_device_operations qmt_lu_ops = {
469 .ldo_prepare = qmt_device_prepare,
470 .ldo_process_config = NULL, /* to be defined for dynamic pool
474 /* global variable initialization called when the lquota module is loaded */
475 int qmt_glb_init(void)
480 rc = class_register_type(&qmt_obd_ops, NULL, true, NULL,
481 LUSTRE_QMT_NAME, &qmt_device_type);
485 /* called when the lquota module is about to be unloaded */
486 void qmt_glb_fini(void)
488 class_unregister_type(LUSTRE_QMT_NAME);