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