Whamcloud - gitweb
LU-2152 iam: it->load fix
[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 & global index copy objects */
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
415         rc = lprocfs_seq_create(qsd->qsd_proc,
416                                 qtype == USRQUOTA ? "limit_user" : "limit_group",
417                                 0444, &lprocfs_quota_seq_fops,
418                                 qqi->qqi_glb_obj);
419         if (rc) {
420                 CERROR("%s: can't add procfs entry for global index copy %d\n",
421                        qsd->qsd_svname, rc);
422                 GOTO(out, rc);
423         }
424         EXIT;
425 out:
426         if (rc)
427                 qsd_qtype_fini(env, qsd, qtype);
428         return rc;
429 }
430
431 /*
432  * Release a qsd_instance. Companion of qsd_init(). This releases all data
433  * structures associated with the quota slave (on-disk objects, lquota entry
434  * tables, ...).
435  * This function should be called when the OSD is shutting down.
436  *
437  * \param env - is the environment passed by the caller
438  * \param qsd - is the qsd instance to shutdown
439  */
440 void qsd_fini(const struct lu_env *env, struct qsd_instance *qsd)
441 {
442         int     qtype;
443         ENTRY;
444
445         if (unlikely(qsd == NULL))
446                 RETURN_EXIT;
447
448         CDEBUG(D_QUOTA, "%s: initiating QSD shutdown\n", qsd->qsd_svname);
449         cfs_write_lock(&qsd->qsd_lock);
450         qsd->qsd_stopping = true;
451         cfs_write_unlock(&qsd->qsd_lock);
452
453         /* remove from the list of fsinfo */
454         if (!cfs_list_empty(&qsd->qsd_link)) {
455                 LASSERT(qsd->qsd_fsinfo != NULL);
456                 cfs_down(&qsd->qsd_fsinfo->qfs_sem);
457                 cfs_list_del_init(&qsd->qsd_link);
458                 cfs_up(&qsd->qsd_fsinfo->qfs_sem);
459         }
460
461         /* remove qsd proc entry */
462         if (qsd->qsd_proc != NULL) {
463                 lprocfs_remove(&qsd->qsd_proc);
464                 qsd->qsd_proc = NULL;
465         }
466
467         /* stop the writeback thread */
468         qsd_stop_upd_thread(qsd);
469
470         /* shutdown the reintegration threads */
471         for (qtype = USRQUOTA; qtype < MAXQUOTAS; qtype++) {
472                 if (qsd->qsd_type_array[qtype] == NULL)
473                         continue;
474                 qsd_stop_reint_thread(qsd->qsd_type_array[qtype]);
475         }
476
477         /* release reference on namespace */
478         if (qsd->qsd_ns != NULL) {
479                 ldlm_namespace_put(qsd->qsd_ns);
480                 qsd->qsd_ns = NULL;
481         }
482
483         /* free per-quota type data */
484         for (qtype = USRQUOTA; qtype < MAXQUOTAS; qtype++)
485                 qsd_qtype_fini(env, qsd, qtype);
486
487         /* deregister connection to the quota master */
488         qsd->qsd_exp_valid = false;
489         lustre_deregister_osp_item(&qsd->qsd_exp);
490
491         /* release per-filesystem information */
492         if (qsd->qsd_fsinfo != NULL)
493                 qsd_put_fsinfo(qsd->qsd_fsinfo);
494
495         /* release quota root directory */
496         if (qsd->qsd_root != NULL) {
497                 lu_object_put(env, &qsd->qsd_root->do_lu);
498                 qsd->qsd_root = NULL;
499         }
500
501         /* release reference on dt_device */
502         if (qsd->qsd_dev != NULL) {
503                 lu_ref_del(&qsd->qsd_dev->dd_lu_dev.ld_reference, "qsd", qsd);
504                 lu_device_put(&qsd->qsd_dev->dd_lu_dev);
505                 qsd->qsd_dev = NULL;
506         }
507
508         CDEBUG(D_QUOTA, "%s: QSD shutdown completed\n", qsd->qsd_svname);
509         OBD_FREE_PTR(qsd);
510         EXIT;
511 }
512 EXPORT_SYMBOL(qsd_fini);
513
514 /*
515  * Create a new qsd_instance to be associated with backend osd device
516  * identified by \dev.
517  *
518  * \param env    - the environment passed by the caller
519  * \param svname - is the service name of the OSD device creating this instance
520  * \param dev    - is the dt_device where to store quota index files
521  * \param osd_proc - is the procfs parent directory where to create procfs file
522  *                   related to this new qsd instance
523  *
524  * \retval - pointer to new qsd_instance associated with dev \dev on success,
525  *           appropriate error on failure
526  */
527 struct qsd_instance *qsd_init(const struct lu_env *env, char *svname,
528                               struct dt_device *dev,
529                               cfs_proc_dir_entry_t *osd_proc)
530 {
531         struct qsd_thread_info  *qti = qsd_info(env);
532         struct qsd_instance     *qsd;
533         int                      rc, type, idx;
534         ENTRY;
535
536         /* only configure qsd for MDT & OST */
537         type = server_name2index(svname, &idx, NULL);
538         if (type != LDD_F_SV_TYPE_MDT && type != LDD_F_SV_TYPE_OST)
539                 RETURN(NULL);
540
541         /* allocate qsd instance */
542         OBD_ALLOC_PTR(qsd);
543         if (qsd == NULL)
544                 RETURN(ERR_PTR(-ENOMEM));
545
546         /* generic initializations */
547         cfs_rwlock_init(&qsd->qsd_lock);
548         CFS_INIT_LIST_HEAD(&qsd->qsd_link);
549         thread_set_flags(&qsd->qsd_upd_thread, SVC_STOPPED);
550         cfs_waitq_init(&qsd->qsd_upd_thread.t_ctl_waitq);
551         CFS_INIT_LIST_HEAD(&qsd->qsd_upd_list);
552         cfs_spin_lock_init(&qsd->qsd_adjust_lock);
553         CFS_INIT_LIST_HEAD(&qsd->qsd_adjust_list);
554         qsd->qsd_prepared = false;
555         qsd->qsd_started = false;
556
557         /* copy service name */
558         strncpy(qsd->qsd_svname, svname, MAX_OBD_NAME);
559
560         /* grab reference on osd device */
561         lu_device_get(&dev->dd_lu_dev);
562         lu_ref_add(&dev->dd_lu_dev.ld_reference, "qsd", qsd);
563         qsd->qsd_dev = dev;
564
565         /* we only support pool ID 0 (default data or metadata pool) for the
566          * time being. A different pool ID could be assigned to this target via
567          * the configuration log in the future */
568         qsd->qsd_pool_id  = 0;
569
570         /* get fsname from svname */
571         rc = server_name2fsname(svname, qti->qti_buf, NULL);
572         if (rc) {
573                 CERROR("%s: fail to extract filesystem name\n", svname);
574                 GOTO(out, rc);
575         }
576
577         /* look up quota setting for the filesystem the target belongs to */
578         qsd->qsd_fsinfo = qsd_get_fsinfo(qti->qti_buf, 1);
579         if (qsd->qsd_fsinfo == NULL) {
580                 CERROR("%s: failed to locate filesystem information\n", svname);
581                 GOTO(out, rc = -EINVAL);
582         }
583
584         /* add in the list of lquota_fsinfo */
585         cfs_down(&qsd->qsd_fsinfo->qfs_sem);
586         list_add_tail(&qsd->qsd_link, &qsd->qsd_fsinfo->qfs_qsd_list);
587         cfs_up(&qsd->qsd_fsinfo->qfs_sem);
588
589         /* register procfs directory */
590         qsd->qsd_proc = lprocfs_register(QSD_DIR, osd_proc,
591                                          lprocfs_quota_qsd_vars, qsd);
592         if (IS_ERR(qsd->qsd_proc)) {
593                 rc = PTR_ERR(qsd->qsd_proc);
594                 qsd->qsd_proc = NULL;
595                 CERROR("%s: fail to create quota slave proc entry (%d)\n",
596                        svname, rc);
597                 GOTO(out, rc);
598         }
599         EXIT;
600 out:
601         if (rc) {
602                 qsd_fini(env, qsd);
603                 return ERR_PTR(rc);
604         }
605         RETURN(qsd);
606 }
607 EXPORT_SYMBOL(qsd_init);
608
609 /*
610  * Initialize on-disk structures in order to manage quota enforcement for
611  * the target associated with the qsd instance \qsd and starts the reintegration
612  * procedure for each quota type as soon as possible.
613  * The last step of the reintegration will be completed once qsd_start() is
614  * called, at which points the space reconciliation with the master will be
615  * executed.
616  * This function must be called when the server stack is fully configured,
617  * typically when ->ldo_prepare is called across the stack.
618  *
619  * \param env - the environment passed by the caller
620  * \param qsd - is qsd_instance to prepare
621  *
622  * \retval - 0 on success, appropriate error on failure
623  */
624 int qsd_prepare(const struct lu_env *env, struct qsd_instance *qsd)
625 {
626         struct qsd_thread_info  *qti = qsd_info(env);
627         int                      qtype, rc = 0;
628         ENTRY;
629
630         if (unlikely(qsd == NULL))
631                 RETURN(0);
632
633         cfs_read_lock(&qsd->qsd_lock);
634         if (qsd->qsd_prepared) {
635                 CERROR("%s: qsd instance already prepared\n", qsd->qsd_svname);
636                 rc = -EALREADY;
637         }
638         cfs_read_unlock(&qsd->qsd_lock);
639         if (rc)
640                 RETURN(rc);
641
642         /* Record whether this qsd instance is managing quota enforcement for a
643          * MDT (i.e. inode quota) or OST (block quota) */
644         if (lu_device_is_md(qsd->qsd_dev->dd_lu_dev.ld_site->ls_top_dev)) {
645                 qsd->qsd_is_md = true;
646                 qsd->qsd_sync_threshold = LQUOTA_LEAST_QUNIT(LQUOTA_RES_MD);
647         } else {
648                 qsd->qsd_sync_threshold = LQUOTA_LEAST_QUNIT(LQUOTA_RES_DT);
649         }
650
651         /* look-up on-disk directory for the quota slave */
652         qsd->qsd_root = lquota_disk_dir_find_create(env, qsd->qsd_dev, NULL,
653                                                     QSD_DIR);
654         if (IS_ERR(qsd->qsd_root)) {
655                 rc = PTR_ERR(qsd->qsd_root);
656                 qsd->qsd_root = NULL;
657                 CERROR("%s: failed to create quota slave root dir (%d)\n",
658                        qsd->qsd_svname, rc);
659                 RETURN(rc);
660         }
661
662         /* initialize per-quota type data */
663         for (qtype = USRQUOTA; qtype < MAXQUOTAS; qtype++) {
664                 rc = qsd_qtype_init(env, qsd, qtype);
665                 if (rc)
666                         RETURN(rc);
667         }
668
669         /* pools successfully setup, mark the qsd as prepared */
670         cfs_write_lock(&qsd->qsd_lock);
671         qsd->qsd_prepared = true;
672         cfs_write_unlock(&qsd->qsd_lock);
673
674         /* start reintegration thread for each type, if required */
675         for (qtype = USRQUOTA; qtype < MAXQUOTAS; qtype++) {
676                 struct qsd_qtype_info   *qqi = qsd->qsd_type_array[qtype];
677
678                 rc = qsd_start_reint_thread(qqi);
679                 if (rc) {
680                         CERROR("%s: failed to start reint thread for type %s "
681                                "(%d)\n", qsd->qsd_svname, QTYPE_NAME(qtype),
682                                rc);
683                         RETURN(rc);
684                 }
685         }
686
687         /* start writeback thread */
688         rc = qsd_start_upd_thread(qsd);
689         if (rc) {
690                 CERROR("%s: failed to start writeback thread (%d)\n",
691                        qsd->qsd_svname, rc);
692                 RETURN(rc);
693         }
694
695         /* generate osp name */
696         rc = tgt_name2ospname((char *)qsd->qsd_svname, qti->qti_buf);
697         if (rc) {
698                 CERROR("%s: failed to generate ospname (%d)\n",
699                        qsd->qsd_svname, rc);
700                 RETURN(rc);
701         }
702
703         /* the connection callback will start the reintegration
704          * procedure if quota is enabled */
705         rc = lustre_register_osp_item(qti->qti_buf, &qsd->qsd_exp,
706                                       qsd_conn_callback, (void *)qsd);
707         if (rc) {
708                 CERROR("%s: fail to get connection to master (%d)\n",
709                        qsd->qsd_svname, rc);
710                 RETURN(rc);
711         }
712
713         RETURN(0);
714 }
715 EXPORT_SYMBOL(qsd_prepare);
716
717 /*
718  * Start a qsd instance. This will complete the last step of the reintegration
719  * procedure as soon as possible (provided that the master is reachable).
720  * This should be called when recovery has been completed and quota should now
721  * be enforced on every operations.
722  *
723  * \param env - the environment passed by the caller
724  * \param qsd - is the qsd instance associated with the osd device to start
725  */
726 int qsd_start(const struct lu_env *env, struct qsd_instance *qsd)
727 {
728         int     type, rc = 0;
729         ENTRY;
730
731         if (unlikely(qsd == NULL))
732                 RETURN(0);
733
734         cfs_write_lock(&qsd->qsd_lock);
735         if (!qsd->qsd_prepared) {
736                 CERROR("%s: can't start qsd instance since it was properly "
737                        "initialized\n", qsd->qsd_svname);
738                 rc = -EFAULT;
739         } else if (qsd->qsd_started) {
740                 CERROR("%s: qsd instance already started\n", qsd->qsd_svname);
741                 rc = -EALREADY;
742         } else {
743                 /* notify that the qsd_instance is now started */
744                 qsd->qsd_started = true;
745         }
746         cfs_write_unlock(&qsd->qsd_lock);
747
748         if (rc)
749                 RETURN(rc);
750
751         /* Trigger the 3rd step of reintegration: If usage > granted, acquire
752          * up to usage; If usage < granted, release down to usage.  */
753         for (type = USRQUOTA; type < MAXQUOTAS; type++) {
754                 struct qsd_qtype_info   *qqi = qsd->qsd_type_array[type];
755                 cfs_waitq_signal(&qqi->qqi_reint_thread.t_ctl_waitq);
756         }
757
758         RETURN(rc);
759 }
760 EXPORT_SYMBOL(qsd_start);
761
762 void lustre_register_quota_process_config(int (*qpc)(struct lustre_cfg *lcfg));
763
764 /*
765  * Global initialization performed at module load time
766  */
767 int qsd_glb_init(void)
768 {
769         int     rc;
770
771         rc = lu_kmem_init(qsd_caches);
772         if (rc)
773                 return rc;
774
775         qsd_key_init_generic(&qsd_thread_key, NULL);
776         lu_context_key_register(&qsd_thread_key);
777         lustre_register_quota_process_config(qsd_process_config);
778
779         return 0;
780 }
781
782 /*
783  * Companion of qsd_glb_init() called at module unload time
784  */
785 void qsd_glb_fini(void)
786 {
787         lustre_register_quota_process_config(NULL);
788         lu_kmem_fini(qsd_caches);
789         lu_context_key_degister(&qsd_thread_key);
790 }