Whamcloud - gitweb
LU-1842 quota: don't setup qsd for standalone MGS
[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 Intel, Inc.
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 #ifndef EXPORT_SYMTAB
53 # define EXPORT_SYMTAB
54 #endif
55
56 #define DEBUG_SUBSYSTEM S_LQUOTA
57
58 #include <obd_class.h>
59 #include "qsd_internal.h"
60
61 cfs_mem_cache_t *upd_kmem;
62
63 struct lu_kmem_descr qsd_caches[] = {
64         {
65                 .ckd_cache = &upd_kmem,
66                 .ckd_name  = "upd_kmem",
67                 .ckd_size  = sizeof(struct qsd_upd_rec)
68         },
69         {
70                 .ckd_cache = NULL
71         }
72 };
73
74 /* define qsd thread key */
75 LU_KEY_INIT_FINI(qsd, struct qsd_thread_info);
76 LU_CONTEXT_KEY_DEFINE(qsd, LCT_MD_THREAD | LCT_DT_THREAD | LCT_LOCAL);
77 LU_KEY_INIT_GENERIC(qsd);
78
79 /* some procfs helpers */
80 static int lprocfs_qsd_rd_state(char *page, char **start, off_t off,
81                                 int count, int *eof, void *data)
82 {
83         struct qsd_instance     *qsd = (struct qsd_instance *)data;
84         char                     enabled[5];
85         int                      rc;
86
87         LASSERT(qsd != NULL);
88
89         memset(enabled, 0, sizeof(enabled));
90         if (qsd_type_enabled(qsd, USRQUOTA))
91                 strcat(enabled, "u");
92         if (qsd_type_enabled(qsd, GRPQUOTA))
93                 strcat(enabled, "g");
94         if (strlen(enabled) == 0)
95                 strcat(enabled, "none");
96
97         rc = snprintf(page, count,
98                       "target name:    %s\n"
99                       "pool ID:        %d\n"
100                       "type:           %s\n"
101                       "quota enabled:  %s\n"
102                       "conn to master: %s\n",
103                       qsd->qsd_svname, qsd->qsd_pool_id,
104                       qsd->qsd_is_md ? "md" : "dt", enabled,
105                       qsd->qsd_exp_valid ? "setup" : "not setup yet");
106
107         if (qsd->qsd_prepared)
108                 rc +=  snprintf(page + rc, count - rc,
109                                 "user uptodate:  glb[%d],slv[%d],reint[%d]\n"
110                                 "group uptodate: glb[%d],slv[%d],reint[%d]\n",
111                                 qsd->qsd_type_array[USRQUOTA]->qqi_glb_uptodate,
112                                 qsd->qsd_type_array[USRQUOTA]->qqi_slv_uptodate,
113                                 qsd->qsd_type_array[USRQUOTA]->qqi_reint,
114                                 qsd->qsd_type_array[GRPQUOTA]->qqi_glb_uptodate,
115                                 qsd->qsd_type_array[GRPQUOTA]->qqi_slv_uptodate,
116                                 qsd->qsd_type_array[GRPQUOTA]->qqi_reint);
117         return rc;
118 }
119
120 static int lprocfs_qsd_rd_enabled(char *page, char **start, off_t off,
121                                   int count, int *eof, void *data)
122 {
123         struct qsd_instance     *qsd = (struct qsd_instance *)data;
124         char                     enabled[5];
125         LASSERT(qsd != NULL);
126
127         memset(enabled, 0, sizeof(enabled));
128         if (qsd_type_enabled(qsd, USRQUOTA))
129                 strcat(enabled, "u");
130         if (qsd_type_enabled(qsd, GRPQUOTA))
131                 strcat(enabled, "g");
132         if (strlen(enabled) == 0)
133                 strcat(enabled, "none");
134
135         return snprintf(page, count, "%s\n", enabled);
136 }
137
138 static struct lprocfs_vars lprocfs_quota_qsd_vars[] = {
139         { "info", lprocfs_qsd_rd_state, 0, 0},
140         { "enabled", lprocfs_qsd_rd_enabled, 0, 0},
141         { NULL }
142 };
143
144 /*
145  * Callback function invoked by the OSP layer when the connection to the master
146  * has been set up.
147  *
148  * \param data - is a pointer to the qsd_instance
149  *
150  * \retval - 0 on success, appropriate error on failure
151  */
152 static int qsd_conn_callback(void *data)
153 {
154         struct qsd_instance *qsd = (struct qsd_instance *)data;
155         int                  type;
156         ENTRY;
157
158         /* qsd_exp should now be valid */
159         LASSERT(qsd->qsd_exp);
160
161         /* grab reference on namespace */
162         ldlm_namespace_get(class_exp2obd(qsd->qsd_exp)->obd_namespace);
163         qsd->qsd_ns = class_exp2obd(qsd->qsd_exp)->obd_namespace;
164
165         cfs_write_lock(&qsd->qsd_lock);
166         /* notify that qsd_exp is now valid */
167         qsd->qsd_exp_valid = true;
168         cfs_write_unlock(&qsd->qsd_lock);
169
170         /* Now that the connection to master is setup, we can initiate the
171          * reintegration procedure for quota types which are enabled.
172          * It is worth noting that, if the qsd_instance hasn't been started
173          * already, then we can only complete the first two steps of the
174          * reintegration procedure (i.e. global lock enqueue and slave
175          * index transfer) since the space usage reconciliation (i.e.
176          * step 3) will have to wait for qsd_start() to be called */
177         for (type = USRQUOTA; type < MAXQUOTAS; type++) {
178                 struct qsd_qtype_info *qqi = qsd->qsd_type_array[type];
179                 cfs_waitq_signal(&qqi->qqi_reint_thread.t_ctl_waitq);
180         }
181
182         RETURN(0);
183 }
184
185 /*
186  * Release qsd_qtype_info structure which contains data associated with a
187  * given quota type. This releases the accounting objects.
188  * It's called on OSD cleanup when the qsd instance is released.
189  *
190  * \param env - is the environment passed by the caller
191  * \param qsd - is the qsd instance managing the qsd_qtype_info structure
192  *              to be released
193  * \param qtype - is the quota type to be shutdown
194  */
195 static void qsd_qtype_fini(const struct lu_env *env, struct qsd_instance *qsd,
196                            int qtype)
197 {
198         struct qsd_qtype_info   *qqi;
199         ENTRY;
200
201         if (qsd->qsd_type_array[qtype] == NULL)
202                 RETURN_EXIT;
203         qqi = qsd->qsd_type_array[qtype];
204         qsd->qsd_type_array[qtype] = NULL;
205
206         /* all deferred work lists should be empty */
207         LASSERT(cfs_list_empty(&qqi->qqi_deferred_glb));
208         LASSERT(cfs_list_empty(&qqi->qqi_deferred_slv));
209
210         /* shutdown lquota site */
211         if (qqi->qqi_site != NULL && !IS_ERR(qqi->qqi_site)) {
212                 lquota_site_free(env, qqi->qqi_site);
213                 qqi->qqi_site = NULL;
214         }
215
216         /* by now, all qqi users should have gone away */
217         LASSERT(cfs_atomic_read(&qqi->qqi_ref) == 1);
218         lu_ref_fini(&qqi->qqi_reference);
219
220         /* release accounting object */
221         if (qqi->qqi_acct_obj != NULL && !IS_ERR(qqi->qqi_acct_obj)) {
222                 lu_object_put(env, &qqi->qqi_acct_obj->do_lu);
223                 qqi->qqi_acct_obj = NULL;
224         }
225
226         /* release slv index */
227         if (qqi->qqi_slv_obj != NULL && !IS_ERR(qqi->qqi_slv_obj)) {
228                 lu_object_put(env, &qqi->qqi_slv_obj->do_lu);
229                 qqi->qqi_slv_obj = NULL;
230                 qqi->qqi_slv_ver = 0;
231         }
232
233         /* release global index */
234         if (qqi->qqi_glb_obj != NULL && !IS_ERR(qqi->qqi_glb_obj)) {
235                 lu_object_put(env, &qqi->qqi_glb_obj->do_lu);
236                 qqi->qqi_glb_obj = NULL;
237                 qqi->qqi_glb_ver = 0;
238         }
239
240         OBD_FREE_PTR(qqi);
241         EXIT;
242 }
243
244 /*
245  * Allocate and initialize a qsd_qtype_info structure for quota type \qtype.
246  * This opens the accounting object and initializes the proc file.
247  * It's called on OSD start when the qsd_prepare() is invoked on the qsd
248  * instance.
249  *
250  * \param env  - the environment passed by the caller
251  * \param qsd  - is the qsd instance which will be in charge of the new
252  *               qsd_qtype_info instance.
253  * \param qtype - is quota type to set up
254  *
255  * \retval - 0 on success and qsd->qsd_type_array[qtype] is allocated,
256  *           appropriate error on failure
257  */
258 static int qsd_qtype_init(const struct lu_env *env, struct qsd_instance *qsd,
259                           int qtype)
260 {
261         struct qsd_qtype_info   *qqi;
262         int                      rc;
263         struct obd_uuid          uuid;
264         ENTRY;
265
266         LASSERT(qsd->qsd_type_array[qtype] == NULL);
267
268         /* allocate structure for this quota type */
269         OBD_ALLOC_PTR(qqi);
270         if (qqi == NULL)
271                 RETURN(-ENOMEM);
272         qsd->qsd_type_array[qtype] = qqi;
273         cfs_atomic_set(&qqi->qqi_ref, 1); /* referenced from qsd */
274
275         /* set backpointer and other parameters */
276         qqi->qqi_qsd   = qsd;
277         qqi->qqi_qtype = qtype;
278         lu_ref_init(&qqi->qqi_reference);
279         lquota_generate_fid(&qqi->qqi_fid, qsd->qsd_pool_id, QSD_RES_TYPE(qsd),
280                             qtype);
281         qqi->qqi_glb_uptodate = false;
282         qqi->qqi_slv_uptodate = false;
283         qqi->qqi_reint        = false;
284         cfs_waitq_init(&qqi->qqi_reint_thread.t_ctl_waitq);
285         thread_set_flags(&qqi->qqi_reint_thread, SVC_STOPPED);
286         CFS_INIT_LIST_HEAD(&qqi->qqi_deferred_glb);
287         CFS_INIT_LIST_HEAD(&qqi->qqi_deferred_slv);
288         memset(&qqi->qqi_lockh, 0, sizeof(qqi->qqi_lockh));
289
290         /* open accounting object */
291         LASSERT(qqi->qqi_acct_obj == NULL);
292         qqi->qqi_acct_obj = acct_obj_lookup(env, qsd->qsd_dev, qtype);
293         if (qqi->qqi_acct_obj == NULL) {
294                 LCONSOLE_ERROR("%s: No %s space accounting support. Please use "
295                                "tunefs.lustre --quota option to enable quota "
296                                "accounting.\n",
297                                qsd->qsd_svname, QTYPE_NAME(qtype));
298                 GOTO(out, rc = -ENOENT);
299         }
300
301         /* open global index copy */
302         LASSERT(qqi->qqi_glb_obj == NULL);
303         qqi->qqi_glb_obj = lquota_disk_glb_find_create(env, qsd->qsd_dev,
304                                                        qsd->qsd_root,
305                                                        &qqi->qqi_fid, true);
306         if (IS_ERR(qqi->qqi_glb_obj)) {
307                 CERROR("%s: can't open global index copy "DFID" %ld\n",
308                        qsd->qsd_svname, PFID(&qqi->qqi_fid),
309                        PTR_ERR(qqi->qqi_glb_obj));
310                 GOTO(out, rc = PTR_ERR(qqi->qqi_glb_obj));
311         }
312         qqi->qqi_glb_ver = dt_version_get(env, qqi->qqi_glb_obj);
313
314         /* open slave index copy */
315         LASSERT(qqi->qqi_slv_obj == NULL);
316         obd_str2uuid(&uuid, qsd->qsd_svname);
317         qqi->qqi_slv_obj = lquota_disk_slv_find_create(env, qsd->qsd_dev,
318                                                        qsd->qsd_root,
319                                                        &qqi->qqi_fid, &uuid,
320                                                        true);
321         if (IS_ERR(qqi->qqi_slv_obj)) {
322                 CERROR("%s: can't open slave index copy "DFID" %ld\n",
323                        qsd->qsd_svname, PFID(&qqi->qqi_fid),
324                        PTR_ERR(qqi->qqi_slv_obj));
325                 GOTO(out, rc = PTR_ERR(qqi->qqi_slv_obj));
326         }
327         qqi->qqi_slv_ver = dt_version_get(env, qqi->qqi_slv_obj);
328
329         /* allocate site */
330         qqi->qqi_site = lquota_site_alloc(env, qqi, false, qtype, &qsd_lqe_ops);
331         if (IS_ERR(qqi->qqi_site)) {
332                 CERROR("%s: can't allocate site "DFID" %ld\n", qsd->qsd_svname,
333                        PFID(&qqi->qqi_fid), PTR_ERR(qqi->qqi_site));
334                 GOTO(out, rc = PTR_ERR(qqi->qqi_site));
335         }
336
337         /* register proc entry for accounting object */
338         rc = lprocfs_seq_create(qsd->qsd_proc,
339                                 qtype == USRQUOTA ? "acct_user" : "acct_group",
340                                 0444, &lprocfs_quota_seq_fops,
341                                 qqi->qqi_acct_obj);
342         if (rc) {
343                 CERROR("%s: can't add procfs entry for accounting file %d\n",
344                        qsd->qsd_svname, rc);
345                 GOTO(out, rc);
346         }
347         EXIT;
348 out:
349         if (rc)
350                 qsd_qtype_fini(env, qsd, qtype);
351         return rc;
352 }
353
354 /*
355  * Release a qsd_instance. Companion of qsd_init(). This releases all data
356  * structures associated with the quota slave (on-disk objects, lquota entry
357  * tables, ...).
358  * This function should be called when the OSD is shutting down.
359  *
360  * \param env - is the environment passed by the caller
361  * \param qsd - is the qsd instance to shutdown
362  */
363 void qsd_fini(const struct lu_env *env, struct qsd_instance *qsd)
364 {
365         int     qtype;
366         ENTRY;
367
368         CDEBUG(D_QUOTA, "%s: initiating QSD shutdown\n", qsd->qsd_svname);
369         cfs_write_lock(&qsd->qsd_lock);
370         qsd->qsd_stopping = true;
371         cfs_write_unlock(&qsd->qsd_lock);
372
373         /* remove from the list of fsinfo */
374         if (!cfs_list_empty(&qsd->qsd_link)) {
375                 LASSERT(qsd->qsd_fsinfo != NULL);
376                 cfs_down(&qsd->qsd_fsinfo->qfs_sem);
377                 cfs_list_del_init(&qsd->qsd_link);
378                 cfs_up(&qsd->qsd_fsinfo->qfs_sem);
379         }
380
381         /* remove qsd proc entry */
382         if (qsd->qsd_proc != NULL) {
383                 lprocfs_remove(&qsd->qsd_proc);
384                 qsd->qsd_proc = NULL;
385         }
386
387         /* stop the writeback thread */
388         qsd_stop_upd_thread(qsd);
389
390         /* shutdown the reintegration threads */
391         for (qtype = USRQUOTA; qtype < MAXQUOTAS; qtype++) {
392                 if (qsd->qsd_type_array[qtype] == NULL)
393                         continue;
394                 qsd_stop_reint_thread(qsd->qsd_type_array[qtype]);
395         }
396
397         /* release reference on namespace */
398         if (qsd->qsd_ns != NULL) {
399                 ldlm_namespace_put(qsd->qsd_ns);
400                 qsd->qsd_ns = NULL;
401         }
402
403         /* free per-quota type data */
404         for (qtype = USRQUOTA; qtype < MAXQUOTAS; qtype++)
405                 qsd_qtype_fini(env, qsd, qtype);
406
407         /* deregister connection to the quota master */
408         qsd->qsd_exp_valid = false;
409         lustre_deregister_osp_item(&qsd->qsd_exp);
410
411         /* release per-filesystem information */
412         if (qsd->qsd_fsinfo != NULL)
413                 qsd_put_fsinfo(qsd->qsd_fsinfo);
414
415         /* release quota root directory */
416         if (qsd->qsd_root != NULL) {
417                 lu_object_put(env, &qsd->qsd_root->do_lu);
418                 qsd->qsd_root = NULL;
419         }
420
421         /* release reference on dt_device */
422         if (qsd->qsd_dev != NULL) {
423                 lu_ref_del(&qsd->qsd_dev->dd_lu_dev.ld_reference, "qsd", qsd);
424                 lu_device_put(&qsd->qsd_dev->dd_lu_dev);
425                 qsd->qsd_dev = NULL;
426         }
427
428         CDEBUG(D_QUOTA, "%s: QSD shutdown completed\n", qsd->qsd_svname);
429         OBD_FREE_PTR(qsd);
430         EXIT;
431 }
432 EXPORT_SYMBOL(qsd_fini);
433
434 /*
435  * Create a new qsd_instance to be associated with backend osd device
436  * identified by \dev.
437  *
438  * \param env    - the environment passed by the caller
439  * \param svname - is the service name of the OSD device creating this instance
440  * \param dev    - is the dt_device where to store quota index files
441  * \param osd_proc - is the procfs parent directory where to create procfs file
442  *                   related to this new qsd instance
443  *
444  * \retval - pointer to new qsd_instance associated with dev \dev on success,
445  *           appropriate error on failure
446  */
447 struct qsd_instance *qsd_init(const struct lu_env *env, char *svname,
448                               struct dt_device *dev,
449                               cfs_proc_dir_entry_t *osd_proc)
450 {
451         struct qsd_thread_info  *qti = qsd_info(env);
452         struct qsd_instance     *qsd;
453         int                      rc, type, idx;
454         ENTRY;
455
456         /* only configure qsd for MDT & OST */
457         type = server_name2index(svname, &idx, NULL);
458         if (type != LDD_F_SV_TYPE_MDT && type != LDD_F_SV_TYPE_OST)
459                 RETURN(NULL);
460
461         /* allocate qsd instance */
462         OBD_ALLOC_PTR(qsd);
463         if (qsd == NULL)
464                 RETURN(ERR_PTR(-ENOMEM));
465
466         /* generic initializations */
467         cfs_rwlock_init(&qsd->qsd_lock);
468         CFS_INIT_LIST_HEAD(&qsd->qsd_link);
469         thread_set_flags(&qsd->qsd_upd_thread, SVC_STOPPED);
470         cfs_waitq_init(&qsd->qsd_upd_thread.t_ctl_waitq);
471         CFS_INIT_LIST_HEAD(&qsd->qsd_upd_list);
472         cfs_spin_lock_init(&qsd->qsd_adjust_lock);
473         CFS_INIT_LIST_HEAD(&qsd->qsd_adjust_list);
474         qsd->qsd_prepared = false;
475         qsd->qsd_started = false;
476
477         /* copy service name */
478         strncpy(qsd->qsd_svname, svname, MAX_OBD_NAME);
479
480         /* grab reference on osd device */
481         lu_device_get(&dev->dd_lu_dev);
482         lu_ref_add(&dev->dd_lu_dev.ld_reference, "qsd", qsd);
483         qsd->qsd_dev = dev;
484
485         /* we only support pool ID 0 (default data or metadata pool) for the
486          * time being. A different pool ID could be assigned to this target via
487          * the configuration log in the future */
488         qsd->qsd_pool_id  = 0;
489
490         /* get fsname from svname */
491         rc = server_name2fsname(svname, qti->qti_buf, NULL);
492         if (rc) {
493                 CERROR("%s: fail to extract filesystem name\n", svname);
494                 GOTO(out, rc);
495         }
496
497         /* look up quota setting for the filesystem the target belongs to */
498         qsd->qsd_fsinfo = qsd_get_fsinfo(qti->qti_buf, 1);
499         if (qsd->qsd_fsinfo == NULL) {
500                 CERROR("%s: failed to locate filesystem information\n", svname);
501                 GOTO(out, rc = -EINVAL);
502         }
503
504         /* add in the list of lquota_fsinfo */
505         cfs_down(&qsd->qsd_fsinfo->qfs_sem);
506         list_add_tail(&qsd->qsd_link, &qsd->qsd_fsinfo->qfs_qsd_list);
507         cfs_up(&qsd->qsd_fsinfo->qfs_sem);
508
509         /* register procfs directory */
510         qsd->qsd_proc = lprocfs_register(QSD_DIR, osd_proc,
511                                          lprocfs_quota_qsd_vars, qsd);
512         if (IS_ERR(qsd->qsd_proc)) {
513                 rc = PTR_ERR(qsd->qsd_proc);
514                 qsd->qsd_proc = NULL;
515                 CERROR("%s: fail to create quota slave proc entry (%d)\n",
516                        svname, rc);
517                 GOTO(out, rc);
518         }
519         EXIT;
520 out:
521         if (rc) {
522                 qsd_fini(env, qsd);
523                 return ERR_PTR(rc);
524         }
525         RETURN(qsd);
526 }
527 EXPORT_SYMBOL(qsd_init);
528
529 /*
530  * Initialize on-disk structures in order to manage quota enforcement for
531  * the target associated with the qsd instance \qsd and starts the reintegration
532  * procedure for each quota type as soon as possible.
533  * The last step of the reintegration will be completed once qsd_start() is
534  * called, at which points the space reconciliation with the master will be
535  * executed.
536  * This function must be called when the server stack is fully configured,
537  * typically when ->ldo_prepare is called across the stack.
538  *
539  * \param env - the environment passed by the caller
540  * \param qsd - is qsd_instance to prepare
541  *
542  * \retval - 0 on success, appropriate error on failure
543  */
544 int qsd_prepare(const struct lu_env *env, struct qsd_instance *qsd)
545 {
546         struct qsd_thread_info  *qti = qsd_info(env);
547         int                      qtype, rc = 0;
548         ENTRY;
549
550         LASSERT(qsd != NULL);
551
552         cfs_read_lock(&qsd->qsd_lock);
553         if (qsd->qsd_prepared) {
554                 CERROR("%s: qsd instance already prepared\n", qsd->qsd_svname);
555                 rc = -EALREADY;
556         }
557         cfs_read_unlock(&qsd->qsd_lock);
558         if (rc)
559                 RETURN(rc);
560
561         /* Record whether this qsd instance is managing quota enforcement for a
562          * MDT (i.e. inode quota) or OST (block quota) */
563         if (lu_device_is_md(qsd->qsd_dev->dd_lu_dev.ld_site->ls_top_dev)) {
564                 qsd->qsd_is_md = true;
565                 qsd->qsd_sync_threshold = LQUOTA_LEAST_QUNIT(LQUOTA_RES_MD);
566         } else {
567                 qsd->qsd_sync_threshold = LQUOTA_LEAST_QUNIT(LQUOTA_RES_DT);
568         }
569
570         /* look-up on-disk directory for the quota slave */
571         qsd->qsd_root = lquota_disk_dir_find_create(env, qsd->qsd_dev, NULL,
572                                                     QSD_DIR);
573         if (IS_ERR(qsd->qsd_root)) {
574                 rc = PTR_ERR(qsd->qsd_root);
575                 qsd->qsd_root = NULL;
576                 CERROR("%s: failed to create quota slave root dir (%d)\n",
577                        qsd->qsd_svname, rc);
578                 RETURN(rc);
579         }
580
581         /* initialize per-quota type data */
582         for (qtype = USRQUOTA; qtype < MAXQUOTAS; qtype++) {
583                 rc = qsd_qtype_init(env, qsd, qtype);
584                 if (rc)
585                         RETURN(rc);
586         }
587
588         /* pools successfully setup, mark the qsd as prepared */
589         cfs_write_lock(&qsd->qsd_lock);
590         qsd->qsd_prepared = true;
591         cfs_write_unlock(&qsd->qsd_lock);
592
593         /* start reintegration thread for each type, if required */
594         for (qtype = USRQUOTA; qtype < MAXQUOTAS; qtype++) {
595                 struct qsd_qtype_info   *qqi = qsd->qsd_type_array[qtype];
596
597                 rc = qsd_start_reint_thread(qqi);
598                 if (rc) {
599                         CERROR("%s: failed to start reint thread for type %s "
600                                "(%d)\n", qsd->qsd_svname, QTYPE_NAME(qtype),
601                                rc);
602                         RETURN(rc);
603                 }
604         }
605
606         /* start writeback thread */
607         rc = qsd_start_upd_thread(qsd);
608         if (rc) {
609                 CERROR("%s: failed to start writeback thread (%d)\n",
610                        qsd->qsd_svname, rc);
611                 RETURN(rc);
612         }
613
614         /* generate osp name */
615         rc = tgt_name2ospname((char *)qsd->qsd_svname, qti->qti_buf);
616         if (rc) {
617                 CERROR("%s: failed to generate ospname (%d)\n",
618                        qsd->qsd_svname, rc);
619                 RETURN(rc);
620         }
621
622         /* the connection callback will start the reintegration
623          * procedure if quota is enabled */
624         rc = lustre_register_osp_item(qti->qti_buf, &qsd->qsd_exp,
625                                       qsd_conn_callback, (void *)qsd);
626         if (rc) {
627                 CERROR("%s: fail to get connection to master (%d)\n",
628                        qsd->qsd_svname, rc);
629                 RETURN(rc);
630         }
631
632         RETURN(0);
633 }
634 EXPORT_SYMBOL(qsd_prepare);
635
636 /*
637  * Start a qsd instance. This will complete the last step of the reintegration
638  * procedure as soon as possible (provided that the master is reachable).
639  * This should be called when recovery has been completed and quota should now
640  * be enforced on every operations.
641  *
642  * \param env - the environment passed by the caller
643  * \param qsd - is the qsd instance associated with the osd device to start
644  */
645 int qsd_start(const struct lu_env *env, struct qsd_instance *qsd)
646 {
647         int     type, rc = 0;
648         ENTRY;
649
650         cfs_write_lock(&qsd->qsd_lock);
651         if (!qsd->qsd_prepared) {
652                 CERROR("%s: can't start qsd instance since it was properly "
653                        "initialized\n", qsd->qsd_svname);
654                 rc = -EFAULT;
655         } else if (qsd->qsd_started) {
656                 CERROR("%s: qsd instance already started\n", qsd->qsd_svname);
657                 rc = -EALREADY;
658         } else {
659                 /* notify that the qsd_instance is now started */
660                 qsd->qsd_started = true;
661         }
662         cfs_write_unlock(&qsd->qsd_lock);
663
664         if (rc)
665                 RETURN(rc);
666
667         /* Trigger the 3rd step of reintegration: If usage > granted, acquire
668          * up to usage; If usage < granted, release down to usage.  */
669         for (type = USRQUOTA; type < MAXQUOTAS; type++) {
670                 struct qsd_qtype_info   *qqi = qsd->qsd_type_array[type];
671                 cfs_waitq_signal(&qqi->qqi_reint_thread.t_ctl_waitq);
672         }
673
674         RETURN(rc);
675 }
676 EXPORT_SYMBOL(qsd_start);
677
678 void lustre_register_quota_process_config(int (*qpc)(struct lustre_cfg *lcfg));
679
680 /*
681  * Global initialization performed at module load time
682  */
683 int qsd_glb_init(void)
684 {
685         int     rc;
686
687         rc = lu_kmem_init(qsd_caches);
688         if (rc)
689                 return rc;
690
691         qsd_key_init_generic(&qsd_thread_key, NULL);
692         lu_context_key_register(&qsd_thread_key);
693         lustre_register_quota_process_config(qsd_process_config);
694
695         return 0;
696 }
697
698 /*
699  * Companion of qsd_glb_init() called at module unload time
700  */
701 void qsd_glb_fini(void)
702 {
703         lustre_register_quota_process_config(NULL);
704         lu_kmem_fini(qsd_caches);
705         lu_context_key_degister(&qsd_thread_key);
706 }