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