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