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