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