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