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