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