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