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