Whamcloud - gitweb
f785ac969696401c9e67dda5ed6eb11a1d52f4d4
[fs/lustre-release.git] / lustre / quota / qmt_dev.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, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 021110-1307, USA
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2012, 2017, Intel Corporation.
25  * Use is subject to license terms.
26  *
27  * Author: Johann Lombardi <johann.lombardi@intel.com>
28  * Author: Niu    Yawei    <yawei.niu@intel.com>
29  */
30
31 /*
32  * Management of the device associated with a Quota Master Target (QMT).
33  *
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.
44  *
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.
49  *
50  * To bring up a QMT device, the following steps must be completed:
51  *
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.
55  *
56  * - the MDT can then connect to the QMT instance via legacy obd_connect path.
57  *
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.
60  *
61  * To shutdown a QMT device, the MDT just has to disconnect from the QMT.
62  *
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.
67  */
68
69 #define DEBUG_SUBSYSTEM S_LQUOTA
70
71 #include <obd_class.h>
72 #include <lprocfs_status.h>
73 #include <lustre_disk.h>
74 #include "qmt_internal.h"
75
76 static const struct lu_device_operations qmt_lu_ops;
77
78 /*
79  * Release quota master target and all data structure associated with this
80  * target.
81  * Called on MDT0 cleanup.
82  *
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
85  *
86  * \retval - NULL on success (backend OSD device is managed by the main stack),
87  *           appropriate error on failure
88  */
89 static struct lu_device *qmt_device_fini(const struct lu_env *env,
90                                          struct lu_device *ld)
91 {
92         struct qmt_device       *qmt = lu2qmt_dev(ld);
93         ENTRY;
94
95         LASSERT(qmt != NULL);
96
97         CDEBUG(D_QUOTA, "%s: initiating QMT shutdown\n", qmt->qmt_svname);
98         qmt->qmt_stopping = true;
99
100         /* kill pool instances, if any */
101         qmt_pool_fini(env, qmt);
102
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;
107         }
108
109         /* stop rebalance thread */
110         if (!qmt->qmt_child->dd_rdonly)
111                 qmt_stop_reba_thread(qmt);
112
113         if (qmt->qmt_root) {
114                 dt_object_put(env, qmt->qmt_root);
115                 qmt->qmt_root = NULL;
116         }
117
118         /* disconnect from OSD */
119         if (qmt->qmt_child_exp != NULL) {
120                 obd_disconnect(qmt->qmt_child_exp);
121                 qmt->qmt_child_exp = NULL;
122                 qmt->qmt_child = NULL;
123         }
124
125         /* clear references to MDT namespace */
126         ld->ld_obd->obd_namespace = NULL;
127         qmt->qmt_ns = NULL;
128
129         RETURN(NULL);
130 }
131
132 /*
133  * Connect a quota master to the backend OSD device.
134  *
135  * \param env - is the environment passed by the caller
136  * \param qmt - is the quota master target to be connected
137  * \param cfg - is the configuration log record from which we need to extract
138  *              the service name of the backend OSD device to connect to.
139  *
140  * \retval - 0 on success, appropriate error on failure
141  */
142 static int qmt_connect_to_osd(const struct lu_env *env, struct qmt_device *qmt,
143                               struct lustre_cfg *cfg)
144 {
145         struct obd_connect_data *data = NULL;
146         struct obd_device       *obd;
147         struct lu_device        *ld = qmt2lu_dev(qmt);
148         int                      rc;
149         ENTRY;
150
151         LASSERT(qmt->qmt_child_exp == NULL);
152
153         OBD_ALLOC_PTR(data);
154         if (data == NULL)
155                 GOTO(out, rc = -ENOMEM);
156
157         /* look-up OBD device associated with the backend OSD device.
158          * The MDT is kind enough to pass the OBD name in QMT configuration */
159         obd = class_name2obd(lustre_cfg_string(cfg, 3));
160         if (obd == NULL) {
161                 CERROR("%s: can't locate backend osd device: %s\n",
162                        qmt->qmt_svname, lustre_cfg_string(cfg, 3));
163                 GOTO(out, rc = -ENOTCONN);
164         }
165
166         data->ocd_connect_flags = OBD_CONNECT_VERSION;
167         data->ocd_version = LUSTRE_VERSION_CODE;
168
169         /* connect to OSD device */
170         rc = obd_connect(NULL, &qmt->qmt_child_exp, obd, &obd->obd_uuid, data,
171                          NULL);
172         if (rc) {
173                 CERROR("%s: cannot connect to osd dev %s (%d)\n",
174                        qmt->qmt_svname, obd->obd_name, rc);
175                 GOTO(out, rc);
176         }
177
178         /* initialize site (although it isn't used anywhere) and lu_device
179          * pointer to next device */
180         qmt->qmt_child = lu2dt_dev(qmt->qmt_child_exp->exp_obd->obd_lu_dev);
181         ld->ld_site = qmt->qmt_child_exp->exp_obd->obd_lu_dev->ld_site;
182         EXIT;
183 out:
184         if (data)
185                 OBD_FREE_PTR(data);
186         return rc;
187 }
188
189 /*
190  * Initialize quota master target device. This includers connecting to
191  * the backend OSD device, initializing the pool configuration and creating the
192  * root procfs directory dedicated to this quota target.
193  * The rest of the initialization is done when the stack is fully configured
194  * (i.e. when ->ldo_start is called across the stack).
195  *
196  * This function is called on MDT0 setup.
197  *
198  * \param env - is the environment passed by the caller
199  * \param qmt - is the quota master target to be initialized
200  * \param ldt - is the device type structure associated with the qmt device
201  * \param cfg - is the configuration record used to configure the qmt device
202  *
203  * \retval - 0 on success, appropriate error on failure
204  */
205 static int qmt_device_init0(const struct lu_env *env, struct qmt_device *qmt,
206                             struct lu_device_type *ldt, struct lustre_cfg *cfg)
207 {
208         struct lu_device        *ld = qmt2lu_dev(qmt);
209         struct obd_device       *obd, *mdt_obd;
210         struct obd_type         *type;
211         char                    *svname = lustre_cfg_string(cfg, 0);
212         int                      rc;
213         ENTRY;
214
215         if (svname == NULL)
216                 RETURN(-EINVAL);
217
218         /* record who i am, it might be useful ... */
219         rc = strlcpy(qmt->qmt_svname, svname, sizeof(qmt->qmt_svname));
220         if (rc >= sizeof(qmt->qmt_svname))
221                 RETURN(-E2BIG);
222
223         /* look-up the obd_device associated with the qmt */
224         obd = class_name2obd(qmt->qmt_svname);
225         if (obd == NULL)
226                 RETURN(-ENOENT);
227
228         /* reference each other */
229         obd->obd_lu_dev = ld;
230         ld->ld_obd      = obd;
231
232         /* look-up the parent MDT to steal its ldlm namespace ... */
233         mdt_obd = class_name2obd(lustre_cfg_string(cfg, 2));
234         if (mdt_obd == NULL)
235                 RETURN(-ENOENT);
236
237         /* borrow  MDT namespace. kind of a hack until we have our own namespace
238          * & service threads */
239         LASSERT(mdt_obd->obd_namespace != NULL);
240         obd->obd_namespace = mdt_obd->obd_namespace;
241         qmt->qmt_ns = obd->obd_namespace;
242
243         /* connect to backend osd device */
244         rc = qmt_connect_to_osd(env, qmt, cfg);
245         if (rc)
246                 GOTO(out, rc);
247
248         /* set up and start rebalance thread */
249         INIT_LIST_HEAD(&qmt->qmt_reba_list);
250         spin_lock_init(&qmt->qmt_reba_lock);
251         if (!qmt->qmt_child->dd_rdonly) {
252                 rc = qmt_start_reba_thread(qmt);
253                 if (rc) {
254                         CERROR("%s: failed to start rebalance thread (%d)\n",
255                                qmt->qmt_svname, rc);
256                         GOTO(out, rc);
257                 }
258         }
259
260         /* at the moment there is no linkage between lu_type and obd_type, so
261          * we lookup obd_type this way */
262         type = class_search_type(LUSTRE_QMT_NAME);
263         LASSERT(type != NULL);
264
265         /* put reference taken by class_search_type */
266         kobject_put(&type->typ_kobj);
267
268         /* register proc directory associated with this qmt */
269         qmt->qmt_proc = lprocfs_register(qmt->qmt_svname, type->typ_procroot,
270                                          NULL, NULL);
271         if (IS_ERR(qmt->qmt_proc)) {
272                 rc = PTR_ERR(qmt->qmt_proc);
273                 CERROR("%s: failed to create qmt proc entry (%d)\n",
274                        qmt->qmt_svname, rc);
275                 GOTO(out, rc);
276         }
277
278         /* initialize pool configuration */
279         rc = qmt_pool_init(env, qmt);
280         if (rc)
281                 GOTO(out, rc);
282
283         EXIT;
284 out:
285         if (rc)
286                 qmt_device_fini(env, ld);
287         return rc;
288 }
289
290 /*
291  * Free quota master target device. Companion of qmt_device_alloc()
292  *
293  * \param env - is the environment passed by the caller
294  * \param ld  - is the lu_device associated with the qmt dev to be freed
295  *
296  * \retval - NULL on success (backend OSD device is managed by the main stack),
297  *           appropriate error on failure
298  */
299 static struct lu_device *qmt_device_free(const struct lu_env *env,
300                                          struct lu_device *ld)
301 {
302         struct qmt_device       *qmt = lu2qmt_dev(ld);
303         ENTRY;
304
305         LASSERT(qmt != NULL);
306
307         lu_device_fini(ld);
308         OBD_FREE_PTR(qmt);
309         RETURN(NULL);
310 }
311
312 /*
313  * Allocate quota master target and initialize it.
314  *
315  * \param env - is the environment passed by the caller
316  * \param ldt - is the device type structure associated with the qmt
317  * \param cfg - is the configuration record used to configure the qmt
318  *
319  * \retval - lu_device structure associated with the qmt on success,
320  *           appropriate error on failure
321  */
322 static struct lu_device *qmt_device_alloc(const struct lu_env *env,
323                                           struct lu_device_type *ldt,
324                                           struct lustre_cfg *cfg)
325 {
326         struct qmt_device       *qmt;
327         struct lu_device        *ld;
328         int                      rc;
329         ENTRY;
330
331         /* allocate qmt device */
332         OBD_ALLOC_PTR(qmt);
333         if (qmt == NULL)
334                 RETURN(ERR_PTR(-ENOMEM));
335
336         /* configure lu/dt_device */
337         ld = qmt2lu_dev(qmt);
338         dt_device_init(&qmt->qmt_dt_dev, ldt);
339         ld->ld_ops = &qmt_lu_ops;
340
341         /* initialize qmt device */
342         rc = qmt_device_init0(env, qmt, ldt, cfg);
343         if (rc != 0) {
344                 qmt_device_free(env, ld);
345                 RETURN(ERR_PTR(rc));
346         }
347
348         RETURN(ld);
349 }
350
351 LU_KEY_INIT_FINI(qmt, struct qmt_thread_info);
352 LU_TYPE_INIT_FINI(qmt, &qmt_thread_key);
353 LU_CONTEXT_KEY_DEFINE(qmt, LCT_MD_THREAD);
354
355 /*
356  * lu device type operations associated with the master target.
357  */
358 static struct lu_device_type_operations qmt_device_type_ops = {
359         .ldto_init              = qmt_type_init,
360         .ldto_fini              = qmt_type_fini,
361
362         .ldto_start             = qmt_type_start,
363         .ldto_stop              = qmt_type_stop,
364
365         .ldto_device_alloc      = qmt_device_alloc,
366         .ldto_device_free       = qmt_device_free,
367
368         .ldto_device_fini       = qmt_device_fini,
369 };
370
371 /*
372  * lu device type structure associated with the master target.
373  * MDT0 uses this structure to configure the qmt.
374  */
375 static struct lu_device_type qmt_device_type = {
376         .ldt_tags       = LU_DEVICE_DT,
377         .ldt_name       = LUSTRE_QMT_NAME,
378         .ldt_ops        = &qmt_device_type_ops,
379         .ldt_ctx_tags   = LCT_MD_THREAD,
380 };
381
382 /*
383  * obd_connect handler used by the MDT to connect to the master target.
384  */
385 static int qmt_device_obd_connect(const struct lu_env *env,
386                                   struct obd_export **exp,
387                                   struct obd_device *obd,
388                                   struct obd_uuid *cluuid,
389                                   struct obd_connect_data *data,
390                                   void *localdata)
391 {
392         struct lustre_handle    conn;
393         int                     rc;
394         ENTRY;
395
396         rc = class_connect(&conn, obd, cluuid);
397         if (rc)
398                 RETURN(rc);
399
400         *exp = class_conn2export(&conn);
401         RETURN(0);
402 }
403
404 /*
405  * obd_disconnect handler used by the MDT to disconnect from the master target.
406  * We trigger cleanup on disconnect since it means that the MDT is about to
407  * shutdown.
408  */
409 static int qmt_device_obd_disconnect(struct obd_export *exp)
410 {
411         struct obd_device       *obd = exp->exp_obd;
412         int                      rc;
413         ENTRY;
414
415         rc = class_disconnect(exp);
416         if (rc)
417                 RETURN(rc);
418
419         rc = class_manual_cleanup(obd);
420         RETURN(0);
421 }
422
423 /*
424  * obd device operations associated with the master target.
425  */
426 static const struct obd_ops qmt_obd_ops = {
427         .o_owner        = THIS_MODULE,
428         .o_connect      = qmt_device_obd_connect,
429         .o_disconnect   = qmt_device_obd_disconnect,
430         .o_pool_new     = qmt_pool_new,
431         .o_pool_rem     = qmt_pool_rem,
432         .o_pool_add     = qmt_pool_add,
433         .o_pool_del     = qmt_pool_del,
434 };
435
436 /*
437  * Called when the MDS is fully configured. We use it to set up local objects
438  * associated with the quota master target.
439  *
440  * \param env - is the environment passed by the caller
441  * \param parent - is the lu_device of the parent, that's to say the mdt
442  * \param ld  - is the lu_device associated with the master target
443  *
444  * \retval    - 0 on success, appropriate error on failure
445  */
446 static int qmt_device_prepare(const struct lu_env *env,
447                               struct lu_device *parent,
448                               struct lu_device *ld)
449 {
450         struct qmt_device       *qmt = lu2qmt_dev(ld);
451         struct dt_object        *qmt_root;
452         int                      rc;
453         ENTRY;
454
455         /* initialize quota master root directory where all index files will be
456          * stored */
457         qmt_root = lquota_disk_dir_find_create(env, qmt->qmt_child, NULL,
458                                                QMT_DIR);
459         if (IS_ERR(qmt_root)) {
460                 rc = PTR_ERR(qmt_root);
461                 CERROR("%s: failed to create master quota directory (%d)\n",
462                        qmt->qmt_svname, rc);
463                 RETURN(rc);
464         }
465
466         qmt->qmt_root = qmt_root;
467         /* initialize on-disk indexes associated with each pool */
468         rc = qmt_pool_prepare(env, qmt, qmt_root, NULL);
469         RETURN(rc);
470 }
471
472 /*
473  * lu device operations for the quota master target
474  */
475 static const struct lu_device_operations qmt_lu_ops = {
476         .ldo_prepare            = qmt_device_prepare,
477         .ldo_process_config     = NULL, /* to be defined for dynamic pool
478                                          * configuration */
479 };
480
481 /* global variable initialization called when the lquota module is loaded */
482 int qmt_glb_init(void)
483 {
484         int rc;
485         ENTRY;
486
487         rc = class_register_type(&qmt_obd_ops, NULL, true, NULL,
488                                  LUSTRE_QMT_NAME, &qmt_device_type);
489         RETURN(rc);
490 }
491
492 /* called when the lquota module is about to be unloaded */
493 void qmt_glb_fini(void)
494 {
495         class_unregister_type(LUSTRE_QMT_NAME);
496 }