Whamcloud - gitweb
LU-12477 lustre: remove obsolete config checks
[fs/lustre-release.git] / lustre / quota / qsd_lib.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  * Quota Slave Driver (QSD) management.
33  *
34  * The quota slave feature is implemented under the form of a library called
35  * QSD. Each OSD device should create a QSD instance via qsd_init() which will
36  * be used to manage quota enforcement for this device. This implies:
37  * - completing the reintegration procedure with the quota master (aka QMT, see
38  *   qmt_dev.c) to retrieve the latest quota settings and space distribution.
39  * - managing quota locks in order to be notified of configuration changes.
40  * - acquiring space from the QMT when quota space for a given user/group is
41  *   close to exhaustion.
42  * - allocating quota space to service threads for local request processing.
43  *
44  * Once the QSD instance created, the OSD device should invoke qsd_start()
45  * when recovery is completed. This notifies the QSD that we are about to
46  * process new requests on which quota should be strictly enforced.
47  * Then, qsd_op_begin/end can be used to reserve/release/pre-acquire quota space
48  * for/after each operation until shutdown where the QSD instance should be
49  * freed via qsd_fini().
50  */
51
52 #define DEBUG_SUBSYSTEM S_LQUOTA
53
54 #include <obd_class.h>
55 #include "qsd_internal.h"
56
57 struct kmem_cache *upd_kmem;
58
59 struct lu_kmem_descr qsd_caches[] = {
60         {
61                 .ckd_cache = &upd_kmem,
62                 .ckd_name  = "upd_kmem",
63                 .ckd_size  = sizeof(struct qsd_upd_rec)
64         },
65         {
66                 .ckd_cache = NULL
67         }
68 };
69
70 /* define qsd thread key */
71 LU_KEY_INIT_FINI(qsd, struct qsd_thread_info);
72 LU_CONTEXT_KEY_DEFINE(qsd, LCT_MD_THREAD | LCT_DT_THREAD | LCT_LOCAL);
73 LU_KEY_INIT_GENERIC(qsd);
74
75 /* some procfs helpers */
76 static int qsd_state_seq_show(struct seq_file *m, void *data)
77 {
78         struct qsd_instance     *qsd = m->private;
79         char                     enabled[5];
80
81         LASSERT(qsd != NULL);
82
83         memset(enabled, 0, sizeof(enabled));
84         if (qsd_type_enabled(qsd, USRQUOTA))
85                 strcat(enabled, "u");
86         if (qsd_type_enabled(qsd, GRPQUOTA))
87                 strcat(enabled, "g");
88         if (qsd_type_enabled(qsd, PRJQUOTA))
89                 strncat(enabled, "p", 1);
90         if (strlen(enabled) == 0)
91                 strcat(enabled, "none");
92
93         /* TODO: further pool ID should be removed or
94          * replaced with pool Name */
95         seq_printf(m, "target name:    %s\n"
96                    "pool ID:        %d\n"
97                    "type:           %s\n"
98                    "quota enabled:  %s\n"
99                    "conn to master: %s\n",
100                    qsd->qsd_svname, 0,
101                    qsd->qsd_is_md ? "md" : "dt", enabled,
102                    qsd->qsd_exp_valid ? "setup" : "not setup yet");
103
104         if (qsd->qsd_prepared) {
105                 memset(enabled, 0, sizeof(enabled));
106                 if (qsd->qsd_type_array[USRQUOTA]->qqi_acct_obj != NULL)
107                         strcat(enabled, "u");
108                 if (qsd->qsd_type_array[GRPQUOTA]->qqi_acct_obj != NULL)
109                         strcat(enabled, "g");
110                 if (qsd->qsd_type_array[PRJQUOTA]->qqi_acct_obj != NULL)
111                         strncat(enabled, "p", 1);
112                 if (strlen(enabled) == 0)
113                         strcat(enabled, "none");
114                 seq_printf(m, "space acct:     %s\n"
115                            "user uptodate:  glb[%d],slv[%d],reint[%d]\n"
116                            "group uptodate: glb[%d],slv[%d],reint[%d]\n"
117                            "project uptodate: glb[%d],slv[%d],reint[%d]\n",
118                            enabled,
119                            qsd->qsd_type_array[USRQUOTA]->qqi_glb_uptodate,
120                            qsd->qsd_type_array[USRQUOTA]->qqi_slv_uptodate,
121                            qsd->qsd_type_array[USRQUOTA]->qqi_reint,
122                            qsd->qsd_type_array[GRPQUOTA]->qqi_glb_uptodate,
123                            qsd->qsd_type_array[GRPQUOTA]->qqi_slv_uptodate,
124                            qsd->qsd_type_array[GRPQUOTA]->qqi_reint,
125                            qsd->qsd_type_array[PRJQUOTA]->qqi_glb_uptodate,
126                            qsd->qsd_type_array[PRJQUOTA]->qqi_slv_uptodate,
127                            qsd->qsd_type_array[PRJQUOTA]->qqi_reint);
128         }
129         return 0;
130 }
131 LPROC_SEQ_FOPS_RO(qsd_state);
132
133 static int qsd_enabled_seq_show(struct seq_file *m, void *data)
134 {
135         struct qsd_instance     *qsd = m->private;
136         char                     enabled[5];
137
138         LASSERT(qsd != NULL);
139
140         memset(enabled, 0, sizeof(enabled));
141         if (qsd_type_enabled(qsd, USRQUOTA))
142                 strncat(enabled, "u", sizeof(enabled) - strlen(enabled));
143         if (qsd_type_enabled(qsd, GRPQUOTA))
144                 strncat(enabled, "g", sizeof(enabled) - strlen(enabled));
145         if (qsd_type_enabled(qsd, PRJQUOTA))
146                 strncat(enabled, "p", sizeof(enabled) - strlen(enabled));
147         if (strlen(enabled) == 0)
148                 strncat(enabled, "none", sizeof(enabled) - strlen(enabled));
149
150         seq_printf(m, "%s\n", enabled);
151         return 0;
152 }
153
154 static ssize_t qsd_enabled_seq_write(struct file *file,
155                                      const char __user *buffer,
156                                      size_t count, loff_t *off)
157 {
158         struct seq_file *m = file->private_data;
159         struct qsd_instance *qsd = m->private;
160         char fsname[LUSTRE_MAXFSNAME + 1];
161         int enabled = 0;
162         char valstr[5];
163         int pool, rc;
164
165         if (count > 4)
166                 return -E2BIG;
167
168         if (copy_from_user(valstr, buffer, count))
169                 GOTO(out, count = -EFAULT);
170
171         valstr[sizeof(valstr) - 1] = 0;
172         if (strchr(valstr, 'u'))
173                 enabled |= BIT(USRQUOTA);
174         if (strchr(valstr, 'g'))
175                 enabled |= BIT(GRPQUOTA);
176         if (strchr(valstr, 'p'))
177                 enabled |= BIT(PRJQUOTA);
178
179         if (enabled == 0 && strcmp(valstr, "none"))
180                 GOTO(out, count = -EINVAL);
181
182         if (qsd->qsd_is_md)
183                 pool = LQUOTA_RES_MD;
184         else
185                 pool = LQUOTA_RES_DT;
186
187         if (server_name2fsname(qsd->qsd_svname, fsname, NULL))
188                 GOTO(out, count = -EINVAL);
189
190         rc = qsd_config(valstr, fsname, pool);
191         if (rc)
192                 count = rc;
193 out:
194         return count;
195 }
196 LPROC_SEQ_FOPS(qsd_enabled);
197
198 /* force reintegration procedure to be executed.
199  * Used for test/debugging purpose */
200 static ssize_t
201 lprocfs_force_reint_seq_write(struct file *file, const char __user *buffer,
202                                 size_t count, loff_t *off)
203 {
204         struct seq_file     *m = file->private_data;
205         struct qsd_instance *qsd = m->private;
206         int                  rc = 0, qtype;
207
208         LASSERT(qsd != NULL);
209
210         write_lock(&qsd->qsd_lock);
211         if (qsd->qsd_stopping) {
212                 /* don't mess up with shutdown procedure, it is already
213                  * complicated enough */
214                 rc = -ESHUTDOWN;
215         } else if (!qsd->qsd_prepared) {
216                 rc = -EAGAIN;
217         } else {
218                 /* mark all indexes as stale */
219                 for (qtype = USRQUOTA; qtype < LL_MAXQUOTAS; qtype++) {
220                         qsd->qsd_type_array[qtype]->qqi_glb_uptodate = false;
221                         qsd->qsd_type_array[qtype]->qqi_slv_uptodate = false;
222                 }
223         }
224         write_unlock(&qsd->qsd_lock);
225
226         if (rc)
227                 return rc;
228
229         /* kick off reintegration */
230         for (qtype = USRQUOTA; qtype < LL_MAXQUOTAS; qtype++) {
231                 rc = qsd_start_reint_thread(qsd->qsd_type_array[qtype]);
232                 if (rc)
233                         break;
234         }
235         return rc == 0 ? count : rc;
236 }
237 LPROC_SEQ_FOPS_WR_ONLY(qsd, force_reint);
238
239 static int qsd_timeout_seq_show(struct seq_file *m, void *data)
240 {
241         struct qsd_instance *qsd = m->private;
242         LASSERT(qsd != NULL);
243
244         seq_printf(m, "%d\n", qsd_wait_timeout(qsd));
245         return 0;
246 }
247
248 static ssize_t
249 qsd_timeout_seq_write(struct file *file, const char __user *buffer,
250                         size_t count, loff_t *off)
251 {
252         struct seq_file *m = file->private_data;
253         struct qsd_instance *qsd = m->private;
254         time64_t timeout;
255         int rc;
256
257         LASSERT(qsd != NULL);
258         rc = kstrtoll_from_user(buffer, count, 0, &timeout);
259         if (rc)
260                 return rc;
261
262         if (timeout < 0)
263                 return -EINVAL;
264
265         qsd->qsd_timeout = timeout;
266         return count;
267 }
268 LPROC_SEQ_FOPS(qsd_timeout);
269
270 static struct lprocfs_vars lprocfs_quota_qsd_vars[] = {
271         { .name =       "info",
272           .fops =       &qsd_state_fops         },
273         { .name =       "enabled",
274           .fops =       &qsd_enabled_fops       },
275         { .name =       "force_reint",
276           .fops =       &qsd_force_reint_fops   },
277         { .name =       "timeout",
278           .fops =       &qsd_timeout_fops       },
279         { NULL }
280 };
281
282 /*
283  * Callback function invoked by the OSP layer when the connection to the master
284  * has been set up.
285  *
286  * \param data - is a pointer to the qsd_instance
287  *
288  * \retval - 0 on success, appropriate error on failure
289  */
290 static int qsd_conn_callback(void *data)
291 {
292         struct qsd_instance *qsd = (struct qsd_instance *)data;
293         int                  type;
294         ENTRY;
295
296         /* qsd_exp should now be valid */
297         LASSERT(qsd->qsd_exp);
298
299         qsd->qsd_ns = class_exp2obd(qsd->qsd_exp)->obd_namespace;
300
301         write_lock(&qsd->qsd_lock);
302         /* notify that qsd_exp is now valid */
303         qsd->qsd_exp_valid = true;
304         write_unlock(&qsd->qsd_lock);
305
306         /* Now that the connection to master is setup, we can initiate the
307          * reintegration procedure for quota types which are enabled.
308          * It is worth noting that, if the qsd_instance hasn't been started
309          * already, then we can only complete the first two steps of the
310          * reintegration procedure (i.e. global lock enqueue and slave
311          * index transfer) since the space usage reconciliation (i.e.
312          * step 3) will have to wait for qsd_start() to be called */
313         for (type = USRQUOTA; type < LL_MAXQUOTAS; type++) {
314                 struct qsd_qtype_info *qqi = qsd->qsd_type_array[type];
315                 wake_up(&qqi->qqi_reint_thread.t_ctl_waitq);
316         }
317
318         RETURN(0);
319 }
320
321 /*
322  * Release qsd_qtype_info structure which contains data associated with a
323  * given quota type. This releases the accounting objects.
324  * It's called on OSD cleanup when the qsd instance is released.
325  *
326  * \param env - is the environment passed by the caller
327  * \param qsd - is the qsd instance managing the qsd_qtype_info structure
328  *              to be released
329  * \param qtype - is the quota type to be shutdown
330  */
331 static void qsd_qtype_fini(const struct lu_env *env, struct qsd_instance *qsd,
332                            int qtype)
333 {
334         struct qsd_qtype_info   *qqi;
335         int repeat = 0;
336         ENTRY;
337
338         if (qsd->qsd_type_array[qtype] == NULL)
339                 RETURN_EXIT;
340         qqi = qsd->qsd_type_array[qtype];
341         qsd->qsd_type_array[qtype] = NULL;
342
343         /* all deferred work lists should be empty */
344         LASSERT(list_empty(&qqi->qqi_deferred_glb));
345         LASSERT(list_empty(&qqi->qqi_deferred_slv));
346
347         /* shutdown lquota site */
348         if (qqi->qqi_site != NULL && !IS_ERR(qqi->qqi_site)) {
349                 lquota_site_free(env, qqi->qqi_site);
350                 qqi->qqi_site = NULL;
351         }
352
353         /* The qqi may still be holding by global locks which are being
354          * canceled asynchronously (LU-4365), see the following steps:
355          *
356          * - On server umount, we try to clear all quota locks first by
357          *   disconnecting LWP (which will invalidate import and cleanup
358          *   all locks on it), however, if quota reint process is holding
359          *   the global lock for reintegration at that time, global lock
360          *   will fail to be cleared on LWP disconnection.
361          *
362          * - Umount process goes on and stops reint process, the global
363          *   lock will be dropped on reint process exit, however, the lock
364          *   cancel in done in asynchronous way, so the
365          *   qsd_glb_blocking_ast() might haven't been called yet when we
366          *   get here.
367          */
368         while (atomic_read(&qqi->qqi_ref) > 1) {
369                 CDEBUG(D_QUOTA, "qqi reference count %u, repeat: %d\n",
370                        atomic_read(&qqi->qqi_ref), repeat);
371                 repeat++;
372                 schedule_timeout_interruptible(cfs_time_seconds(1));
373         }
374
375         /* by now, all qqi users should have gone away */
376         LASSERT(atomic_read(&qqi->qqi_ref) == 1);
377         lu_ref_fini(&qqi->qqi_reference);
378
379         /* release accounting object */
380         if (qqi->qqi_acct_obj != NULL && !IS_ERR(qqi->qqi_acct_obj)) {
381                 dt_object_put(env, qqi->qqi_acct_obj);
382                 qqi->qqi_acct_obj = NULL;
383         }
384
385         /* release slv index */
386         if (qqi->qqi_slv_obj != NULL && !IS_ERR(qqi->qqi_slv_obj)) {
387                 dt_object_put(env, qqi->qqi_slv_obj);
388                 qqi->qqi_slv_obj = NULL;
389                 qqi->qqi_slv_ver = 0;
390         }
391
392         /* release global index */
393         if (qqi->qqi_glb_obj != NULL && !IS_ERR(qqi->qqi_glb_obj)) {
394                 dt_object_put(env, qqi->qqi_glb_obj);
395                 qqi->qqi_glb_obj = NULL;
396                 qqi->qqi_glb_ver = 0;
397         }
398
399         OBD_FREE_PTR(qqi);
400         EXIT;
401 }
402
403 static const char *qtype2acct_name(int qtype)
404 {
405         static char unknown[24];
406
407         switch (qtype) {
408         case USRQUOTA:
409                 return "acct_user";
410         case GRPQUOTA:
411                 return "acct_group";
412         case PRJQUOTA:
413                 return "acct_project";
414         }
415
416         snprintf(unknown, sizeof(unknown), "acct_unknown_%u", qtype);
417         return unknown;
418 }
419
420 static const char *qtype2glb_name(int qtype)
421 {
422         static char unknown[24];
423
424         switch (qtype) {
425         case USRQUOTA:
426                 return "limit_user";
427         case GRPQUOTA:
428                 return "limit_group";
429         case PRJQUOTA:
430                 return "limit_project";
431         }
432
433         snprintf(unknown, sizeof(unknown), "acct_unknown_%u", qtype);
434         return unknown;
435 }
436
437 /*
438  * Allocate and initialize a qsd_qtype_info structure for quota type \qtype.
439  * This opens the accounting object and initializes the proc file.
440  * It's called on OSD start when the qsd_prepare() is invoked on the qsd
441  * instance.
442  *
443  * \param env  - the environment passed by the caller
444  * \param qsd  - is the qsd instance which will be in charge of the new
445  *               qsd_qtype_info instance.
446  * \param qtype - is quota type to set up
447  *
448  * \retval - 0 on success and qsd->qsd_type_array[qtype] is allocated,
449  *           appropriate error on failure
450  */
451 static int qsd_qtype_init(const struct lu_env *env, struct qsd_instance *qsd,
452                           int qtype)
453 {
454         struct qsd_qtype_info   *qqi;
455         int                      rc;
456         struct obd_uuid          uuid;
457         ENTRY;
458
459         LASSERT(qsd->qsd_type_array[qtype] == NULL);
460
461         /* allocate structure for this quota type */
462         OBD_ALLOC_PTR(qqi);
463         if (qqi == NULL)
464                 RETURN(-ENOMEM);
465         qsd->qsd_type_array[qtype] = qqi;
466         atomic_set(&qqi->qqi_ref, 1); /* referenced from qsd */
467
468         /* set backpointer and other parameters */
469         qqi->qqi_qsd   = qsd;
470         qqi->qqi_qtype = qtype;
471         lu_ref_init(&qqi->qqi_reference);
472         qqi->qqi_glb_uptodate = false;
473         qqi->qqi_slv_uptodate = false;
474         qqi->qqi_reint        = false;
475         init_waitqueue_head(&qqi->qqi_reint_thread.t_ctl_waitq);
476         thread_set_flags(&qqi->qqi_reint_thread, SVC_STOPPED);
477         INIT_LIST_HEAD(&qqi->qqi_deferred_glb);
478         INIT_LIST_HEAD(&qqi->qqi_deferred_slv);
479         lquota_generate_fid(&qqi->qqi_fid, QSD_RES_TYPE(qsd), qtype);
480
481         /* open accounting object */
482         LASSERT(qqi->qqi_acct_obj == NULL);
483         qqi->qqi_acct_obj = acct_obj_lookup(env, qsd->qsd_dev, qtype);
484         if (IS_ERR(qqi->qqi_acct_obj)) {
485                 CDEBUG(D_QUOTA, "%s: no %s space accounting support: rc = %ld\n",
486                        qsd->qsd_svname, qtype_name(qtype),
487                        PTR_ERR(qqi->qqi_acct_obj));
488                 qqi->qqi_acct_obj = NULL;
489                 qqi->qqi_acct_failed = true;
490         }
491
492         /* open global index copy */
493         LASSERT(qqi->qqi_glb_obj == NULL);
494         qqi->qqi_glb_obj = lquota_disk_glb_find_create(env, qsd->qsd_dev,
495                                                        qsd->qsd_root,
496                                                        &qqi->qqi_fid, true);
497         if (IS_ERR(qqi->qqi_glb_obj)) {
498                 CERROR("%s: can't open global index copy "DFID" %ld\n",
499                        qsd->qsd_svname, PFID(&qqi->qqi_fid),
500                        PTR_ERR(qqi->qqi_glb_obj));
501                 GOTO(out, rc = PTR_ERR(qqi->qqi_glb_obj));
502         }
503         qqi->qqi_glb_ver = dt_version_get(env, qqi->qqi_glb_obj);
504
505         /* open slave index copy */
506         LASSERT(qqi->qqi_slv_obj == NULL);
507         obd_str2uuid(&uuid, qsd->qsd_svname);
508         qqi->qqi_slv_obj = lquota_disk_slv_find_create(env, qsd->qsd_dev,
509                                                        qsd->qsd_root,
510                                                        &qqi->qqi_fid, &uuid,
511                                                        true);
512         if (IS_ERR(qqi->qqi_slv_obj)) {
513                 CERROR("%s: can't open slave index copy "DFID" %ld\n",
514                        qsd->qsd_svname, PFID(&qqi->qqi_fid),
515                        PTR_ERR(qqi->qqi_slv_obj));
516                 GOTO(out, rc = PTR_ERR(qqi->qqi_slv_obj));
517         }
518         qqi->qqi_slv_ver = dt_version_get(env, qqi->qqi_slv_obj);
519
520         /* allocate site */
521         qqi->qqi_site = lquota_site_alloc(env, qqi, false, qtype, &qsd_lqe_ops);
522         if (IS_ERR(qqi->qqi_site)) {
523                 CERROR("%s: can't allocate site "DFID" %ld\n", qsd->qsd_svname,
524                        PFID(&qqi->qqi_fid), PTR_ERR(qqi->qqi_site));
525                 GOTO(out, rc = PTR_ERR(qqi->qqi_site));
526         }
527
528         /* register proc entry for accounting & global index copy objects */
529         rc = lprocfs_seq_create(qsd->qsd_proc, qtype2acct_name(qtype),
530                                 0444, &lprocfs_quota_seq_fops,
531                                 qqi->qqi_acct_obj);
532         if (rc) {
533                 CERROR("%s: can't add procfs entry for accounting file %d\n",
534                        qsd->qsd_svname, rc);
535                 GOTO(out, rc);
536         }
537
538         rc = lprocfs_seq_create(qsd->qsd_proc, qtype2glb_name(qtype),
539                                 0444, &lprocfs_quota_seq_fops,
540                                 qqi->qqi_glb_obj);
541         if (rc) {
542                 CERROR("%s: can't add procfs entry for global index copy %d\n",
543                        qsd->qsd_svname, rc);
544                 GOTO(out, rc);
545         }
546         EXIT;
547 out:
548         if (rc)
549                 qsd_qtype_fini(env, qsd, qtype);
550         return rc;
551 }
552
553 /*
554  * Release a qsd_instance. Companion of qsd_init(). This releases all data
555  * structures associated with the quota slave (on-disk objects, lquota entry
556  * tables, ...).
557  * This function should be called when the OSD is shutting down.
558  *
559  * \param env - is the environment passed by the caller
560  * \param qsd - is the qsd instance to shutdown
561  */
562 void qsd_fini(const struct lu_env *env, struct qsd_instance *qsd)
563 {
564         int     qtype;
565         ENTRY;
566
567         if (unlikely(qsd == NULL))
568                 RETURN_EXIT;
569
570         CDEBUG(D_QUOTA, "%s: initiating QSD shutdown\n", qsd->qsd_svname);
571         write_lock(&qsd->qsd_lock);
572         qsd->qsd_stopping = true;
573         write_unlock(&qsd->qsd_lock);
574
575         /* remove qsd proc entry */
576         if (qsd->qsd_proc != NULL) {
577                 lprocfs_remove(&qsd->qsd_proc);
578                 qsd->qsd_proc = NULL;
579         }
580
581         /* stop the writeback thread */
582         qsd_stop_upd_thread(qsd);
583
584         /* shutdown the reintegration threads */
585         for (qtype = USRQUOTA; qtype < LL_MAXQUOTAS; qtype++) {
586                 if (qsd->qsd_type_array[qtype] == NULL)
587                         continue;
588                 qsd_stop_reint_thread(qsd->qsd_type_array[qtype]);
589         }
590
591         if (qsd->qsd_ns != NULL) {
592                 qsd->qsd_ns = NULL;
593         }
594
595         /* release per-filesystem information */
596         if (qsd->qsd_fsinfo != NULL) {
597                 mutex_lock(&qsd->qsd_fsinfo->qfs_mutex);
598                 /* remove from the list of fsinfo */
599                 list_del_init(&qsd->qsd_link);
600                 mutex_unlock(&qsd->qsd_fsinfo->qfs_mutex);
601                 qsd_put_fsinfo(qsd->qsd_fsinfo);
602                 qsd->qsd_fsinfo = NULL;
603         }
604
605         /* free per-quota type data */
606         for (qtype = USRQUOTA; qtype < LL_MAXQUOTAS; qtype++)
607                 qsd_qtype_fini(env, qsd, qtype);
608
609         /* deregister connection to the quota master */
610         qsd->qsd_exp_valid = false;
611         lustre_deregister_lwp_item(&qsd->qsd_exp);
612
613         /* release quota root directory */
614         if (qsd->qsd_root != NULL) {
615                 dt_object_put(env, qsd->qsd_root);
616                 qsd->qsd_root = NULL;
617         }
618
619         /* release reference on dt_device */
620         if (qsd->qsd_dev != NULL) {
621                 lu_ref_del(&qsd->qsd_dev->dd_lu_dev.ld_reference, "qsd", qsd);
622                 lu_device_put(&qsd->qsd_dev->dd_lu_dev);
623                 qsd->qsd_dev = NULL;
624         }
625
626         CDEBUG(D_QUOTA, "%s: QSD shutdown completed\n", qsd->qsd_svname);
627         OBD_FREE_PTR(qsd);
628         EXIT;
629 }
630 EXPORT_SYMBOL(qsd_fini);
631
632 /*
633  * Create a new qsd_instance to be associated with backend osd device
634  * identified by \dev.
635  *
636  * \param env    - the environment passed by the caller
637  * \param svname - is the service name of the OSD device creating this instance
638  * \param dev    - is the dt_device where to store quota index files
639  * \param osd_proc - is the procfs parent directory where to create procfs file
640  *                   related to this new qsd instance
641  *
642  * \retval - pointer to new qsd_instance associated with dev \dev on success,
643  *           appropriate error on failure
644  */
645 struct qsd_instance *qsd_init(const struct lu_env *env, char *svname,
646                               struct dt_device *dev,
647                               struct proc_dir_entry *osd_proc, bool is_md)
648 {
649         struct qsd_thread_info  *qti = qsd_info(env);
650         struct qsd_instance     *qsd;
651         int                      rc, type, idx;
652         ENTRY;
653
654         /* only configure qsd for MDT & OST */
655         type = server_name2index(svname, &idx, NULL);
656         if (type != LDD_F_SV_TYPE_MDT && type != LDD_F_SV_TYPE_OST)
657                 RETURN(NULL);
658
659         /* allocate qsd instance */
660         OBD_ALLOC_PTR(qsd);
661         if (qsd == NULL)
662                 RETURN(ERR_PTR(-ENOMEM));
663
664         /* generic initializations */
665         rwlock_init(&qsd->qsd_lock);
666         INIT_LIST_HEAD(&qsd->qsd_link);
667         thread_set_flags(&qsd->qsd_upd_thread, SVC_STOPPED);
668         init_waitqueue_head(&qsd->qsd_upd_thread.t_ctl_waitq);
669         INIT_LIST_HEAD(&qsd->qsd_upd_list);
670         spin_lock_init(&qsd->qsd_adjust_lock);
671         INIT_LIST_HEAD(&qsd->qsd_adjust_list);
672         qsd->qsd_prepared = false;
673         qsd->qsd_started = false;
674         qsd->qsd_is_md = is_md;
675
676         /* copy service name */
677         if (strlcpy(qsd->qsd_svname, svname, sizeof(qsd->qsd_svname))
678             >= sizeof(qsd->qsd_svname))
679                 GOTO(out, rc = -E2BIG);
680
681         /* grab reference on osd device */
682         lu_device_get(&dev->dd_lu_dev);
683         lu_ref_add(&dev->dd_lu_dev.ld_reference, "qsd", qsd);
684         qsd->qsd_dev = dev;
685
686         /* get fsname from svname */
687         rc = server_name2fsname(svname, qti->qti_buf, NULL);
688         if (rc) {
689                 CERROR("%s: fail to extract filesystem name\n", svname);
690                 GOTO(out, rc);
691         }
692
693         /* look up quota setting for the filesystem the target belongs to */
694         qsd->qsd_fsinfo = qsd_get_fsinfo(qti->qti_buf, 1);
695         if (qsd->qsd_fsinfo == NULL) {
696                 CERROR("%s: failed to locate filesystem information\n", svname);
697                 GOTO(out, rc = -EINVAL);
698         }
699
700         /* add in the list of lquota_fsinfo */
701         mutex_lock(&qsd->qsd_fsinfo->qfs_mutex);
702         list_add_tail(&qsd->qsd_link, &qsd->qsd_fsinfo->qfs_qsd_list);
703         mutex_unlock(&qsd->qsd_fsinfo->qfs_mutex);
704
705         /* register procfs directory */
706         if (qsd->qsd_is_md)
707                 qsd->qsd_proc = lprocfs_register(QSD_DIR_MD, osd_proc,
708                                                  lprocfs_quota_qsd_vars, qsd);
709         else
710                 qsd->qsd_proc = lprocfs_register(QSD_DIR_DT, osd_proc,
711                                                  lprocfs_quota_qsd_vars, qsd);
712
713         if (type == LDD_F_SV_TYPE_MDT && qsd->qsd_is_md)
714                 lprocfs_add_symlink(QSD_DIR, osd_proc, "./%s", QSD_DIR_MD);
715         else if (type == LDD_F_SV_TYPE_OST && !qsd->qsd_is_md)
716                 lprocfs_add_symlink(QSD_DIR, osd_proc, "./%s", QSD_DIR_DT);
717
718         if (IS_ERR(qsd->qsd_proc)) {
719                 rc = PTR_ERR(qsd->qsd_proc);
720                 qsd->qsd_proc = NULL;
721                 CERROR("%s: fail to create quota slave proc entry (%d)\n",
722                        svname, rc);
723                 GOTO(out, rc);
724         }
725         EXIT;
726 out:
727         if (rc) {
728                 qsd_fini(env, qsd);
729                 return ERR_PTR(rc);
730         }
731         RETURN(qsd);
732 }
733 EXPORT_SYMBOL(qsd_init);
734
735 /*
736  * Initialize on-disk structures in order to manage quota enforcement for
737  * the target associated with the qsd instance \qsd and starts the reintegration
738  * procedure for each quota type as soon as possible.
739  * The last step of the reintegration will be completed once qsd_start() is
740  * called, at which points the space reconciliation with the master will be
741  * executed.
742  * This function must be called when the server stack is fully configured,
743  * typically when ->ldo_prepare is called across the stack.
744  *
745  * \param env - the environment passed by the caller
746  * \param qsd - is qsd_instance to prepare
747  *
748  * \retval - 0 on success, appropriate error on failure
749  */
750 int qsd_prepare(const struct lu_env *env, struct qsd_instance *qsd)
751 {
752         struct qsd_thread_info  *qti = qsd_info(env);
753         int                      qtype, rc = 0;
754         ENTRY;
755
756         if (unlikely(qsd == NULL))
757                 RETURN(0);
758
759         read_lock(&qsd->qsd_lock);
760         if (qsd->qsd_prepared) {
761                 CERROR("%s: qsd instance already prepared\n", qsd->qsd_svname);
762                 rc = -EALREADY;
763         }
764         read_unlock(&qsd->qsd_lock);
765         if (rc)
766                 RETURN(rc);
767
768         /* Record whether this qsd instance is managing quota enforcement for a
769          * MDT (i.e. inode quota) or OST (block quota) */
770         if (qsd->qsd_is_md)
771                 qsd->qsd_sync_threshold = LQUOTA_LEAST_QUNIT(LQUOTA_RES_MD);
772         else
773                 qsd->qsd_sync_threshold = LQUOTA_LEAST_QUNIT(LQUOTA_RES_DT);
774
775         /* look-up on-disk directory for the quota slave */
776         qsd->qsd_root = lquota_disk_dir_find_create(env, qsd->qsd_dev, NULL,
777                                                     QSD_DIR);
778         if (IS_ERR(qsd->qsd_root)) {
779                 rc = PTR_ERR(qsd->qsd_root);
780                 qsd->qsd_root = NULL;
781                 CERROR("%s: failed to create quota slave root dir (%d)\n",
782                        qsd->qsd_svname, rc);
783                 RETURN(rc);
784         }
785
786         /* initialize per-quota type data */
787         for (qtype = USRQUOTA; qtype < LL_MAXQUOTAS; qtype++) {
788                 rc = qsd_qtype_init(env, qsd, qtype);
789                 if (rc)
790                         RETURN(rc);
791         }
792
793         /* pools successfully setup, mark the qsd as prepared */
794         write_lock(&qsd->qsd_lock);
795         qsd->qsd_prepared = true;
796         write_unlock(&qsd->qsd_lock);
797
798         if (qsd->qsd_dev->dd_rdonly)
799                 RETURN(0);
800
801         /* start reintegration thread for each type, if required */
802         for (qtype = USRQUOTA; qtype < LL_MAXQUOTAS; qtype++) {
803                 struct qsd_qtype_info   *qqi = qsd->qsd_type_array[qtype];
804
805                 if (qsd_type_enabled(qsd, qtype) &&
806                     qqi->qqi_acct_failed) {
807                         LCONSOLE_ERROR("%s: can't enable quota enforcement "
808                                        "since space accounting isn't functional"
809                                        ". Please run tunefs.lustre --quota on "
810                                        "an unmounted filesystem if not done "
811                                        "already\n", qsd->qsd_svname);
812                         continue;
813                 }
814
815                 rc = qsd_start_reint_thread(qqi);
816                 if (rc) {
817                         CERROR("%s: failed to start reint thread for type %s: rc = %d\n",
818                                 qsd->qsd_svname, qtype_name(qtype), rc);
819                         RETURN(rc);
820                 }
821         }
822
823         /* start writeback thread */
824         rc = qsd_start_upd_thread(qsd);
825         if (rc) {
826                 CERROR("%s: failed to start writeback thread (%d)\n",
827                        qsd->qsd_svname, rc);
828                 RETURN(rc);
829         }
830
831         /* generate osp name */
832         rc = tgt_name2lwp_name(qsd->qsd_svname, qti->qti_buf,
833                                MTI_NAME_MAXLEN, 0);
834         if (rc) {
835                 CERROR("%s: failed to generate ospname (%d)\n",
836                        qsd->qsd_svname, rc);
837                 RETURN(rc);
838         }
839
840         /* the connection callback will start the reintegration
841          * procedure if quota is enabled */
842         rc = lustre_register_lwp_item(qti->qti_buf, &qsd->qsd_exp,
843                                       qsd_conn_callback, (void *)qsd);
844         if (rc) {
845                 CERROR("%s: fail to get connection to master (%d)\n",
846                        qsd->qsd_svname, rc);
847                 RETURN(rc);
848         }
849
850         RETURN(0);
851 }
852 EXPORT_SYMBOL(qsd_prepare);
853
854 /*
855  * Start a qsd instance. This will complete the last step of the reintegration
856  * procedure as soon as possible (provided that the master is reachable).
857  * This should be called when recovery has been completed and quota should now
858  * be enforced on every operations.
859  *
860  * \param env - the environment passed by the caller
861  * \param qsd - is the qsd instance associated with the osd device to start
862  */
863 int qsd_start(const struct lu_env *env, struct qsd_instance *qsd)
864 {
865         int     type, rc = 0;
866         ENTRY;
867
868         if (unlikely(qsd == NULL))
869                 RETURN(0);
870
871         write_lock(&qsd->qsd_lock);
872         if (!qsd->qsd_prepared) {
873                 CERROR("%s: can't start qsd instance since it wasn't properly "
874                        "initialized\n", qsd->qsd_svname);
875                 rc = -EFAULT;
876         } else if (qsd->qsd_started) {
877                 CERROR("%s: qsd instance already started\n", qsd->qsd_svname);
878                 rc = -EALREADY;
879         } else {
880                 /* notify that the qsd_instance is now started */
881                 qsd->qsd_started = true;
882         }
883         write_unlock(&qsd->qsd_lock);
884
885         if (rc)
886                 RETURN(rc);
887
888         /* Trigger the 3rd step of reintegration: If usage > granted, acquire
889          * up to usage; If usage < granted, release down to usage.  */
890         for (type = USRQUOTA; type < LL_MAXQUOTAS; type++) {
891                 struct qsd_qtype_info   *qqi = qsd->qsd_type_array[type];
892                 wake_up(&qqi->qqi_reint_thread.t_ctl_waitq);
893         }
894
895         RETURN(rc);
896 }
897 EXPORT_SYMBOL(qsd_start);
898
899 void lustre_register_quota_process_config(int (*qpc)(struct lustre_cfg *lcfg));
900
901 /*
902  * Global initialization performed at module load time
903  */
904 int qsd_glb_init(void)
905 {
906         int     rc;
907
908         rc = lu_kmem_init(qsd_caches);
909         if (rc)
910                 return rc;
911
912         qsd_key_init_generic(&qsd_thread_key, NULL);
913         lu_context_key_register(&qsd_thread_key);
914         lustre_register_quota_process_config(qsd_process_config);
915
916         return 0;
917 }
918
919 /*
920  * Companion of qsd_glb_init() called at module unload time
921  */
922 void qsd_glb_fini(void)
923 {
924         lustre_register_quota_process_config(NULL);
925         lu_kmem_fini(qsd_caches);
926         lu_context_key_degister(&qsd_thread_key);
927 }