4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
24 * Copyright (c) 2012, 2017, Intel Corporation.
25 * Use is subject to license terms.
27 * Author: Johann Lombardi <johann.lombardi@intel.com>
28 * Author: Niu Yawei <yawei.niu@intel.com>
32 * Quota Slave Driver (QSD) management.
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.
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().
52 #define DEBUG_SUBSYSTEM S_LQUOTA
54 #include <obd_class.h>
55 #include "qsd_internal.h"
57 struct kmem_cache *upd_kmem;
59 struct lu_kmem_descr qsd_caches[] = {
61 .ckd_cache = &upd_kmem,
62 .ckd_name = "upd_kmem",
63 .ckd_size = sizeof(struct qsd_upd_rec)
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);
75 /* some procfs helpers */
76 static int qsd_state_seq_show(struct seq_file *m, void *data)
78 struct qsd_instance *qsd = m->private;
83 memset(enabled, 0, sizeof(enabled));
84 if (qsd_type_enabled(qsd, USRQUOTA))
86 if (qsd_type_enabled(qsd, GRPQUOTA))
88 if (qsd_type_enabled(qsd, PRJQUOTA))
89 strncat(enabled, "p", 1);
90 if (strlen(enabled) == 0)
91 strcat(enabled, "none");
93 /* TODO: further pool ID should be removed or
94 * replaced with pool Name */
95 seq_printf(m, "target name: %s\n"
99 "conn to master: %s\n",
101 qsd->qsd_is_md ? "md" : "dt", enabled,
102 qsd->qsd_exp_valid ? "setup" : "not setup yet");
104 if (qsd->qsd_prepared) {
105 memset(enabled, 0, sizeof(enabled));
106 if (qsd->qsd_type_array[USRQUOTA]->qqi_acct_obj != NULL)
107 strcat(enabled, "u");
108 if (qsd->qsd_type_array[GRPQUOTA]->qqi_acct_obj != NULL)
109 strcat(enabled, "g");
110 if (qsd->qsd_type_array[PRJQUOTA]->qqi_acct_obj != NULL)
111 strncat(enabled, "p", 1);
112 if (strlen(enabled) == 0)
113 strcat(enabled, "none");
114 seq_printf(m, "space acct: %s\n"
115 "user uptodate: glb[%d],slv[%d],reint[%d]\n"
116 "group uptodate: glb[%d],slv[%d],reint[%d]\n"
117 "project uptodate: glb[%d],slv[%d],reint[%d]\n",
119 qsd->qsd_type_array[USRQUOTA]->qqi_glb_uptodate,
120 qsd->qsd_type_array[USRQUOTA]->qqi_slv_uptodate,
121 qsd->qsd_type_array[USRQUOTA]->qqi_reint,
122 qsd->qsd_type_array[GRPQUOTA]->qqi_glb_uptodate,
123 qsd->qsd_type_array[GRPQUOTA]->qqi_slv_uptodate,
124 qsd->qsd_type_array[GRPQUOTA]->qqi_reint,
125 qsd->qsd_type_array[PRJQUOTA]->qqi_glb_uptodate,
126 qsd->qsd_type_array[PRJQUOTA]->qqi_slv_uptodate,
127 qsd->qsd_type_array[PRJQUOTA]->qqi_reint);
131 LPROC_SEQ_FOPS_RO(qsd_state);
133 static int qsd_enabled_seq_show(struct seq_file *m, void *data)
135 struct qsd_instance *qsd = m->private;
138 LASSERT(qsd != NULL);
140 memset(enabled, 0, sizeof(enabled));
141 if (qsd_type_enabled(qsd, USRQUOTA))
142 strncat(enabled, "u", sizeof(enabled) - strlen(enabled));
143 if (qsd_type_enabled(qsd, GRPQUOTA))
144 strncat(enabled, "g", sizeof(enabled) - strlen(enabled));
145 if (qsd_type_enabled(qsd, PRJQUOTA))
146 strncat(enabled, "p", sizeof(enabled) - strlen(enabled));
147 if (strlen(enabled) == 0)
148 strncat(enabled, "none", sizeof(enabled) - strlen(enabled));
150 seq_printf(m, "%s\n", enabled);
154 static ssize_t qsd_enabled_seq_write(struct file *file,
155 const char __user *buffer,
156 size_t count, loff_t *off)
158 struct seq_file *m = file->private_data;
159 struct qsd_instance *qsd = m->private;
160 char fsname[LUSTRE_MAXFSNAME + 1];
168 if (copy_from_user(valstr, buffer, count))
169 GOTO(out, count = -EFAULT);
171 valstr[sizeof(valstr) - 1] = 0;
172 if (strchr(valstr, 'u'))
173 enabled |= BIT(USRQUOTA);
174 if (strchr(valstr, 'g'))
175 enabled |= BIT(GRPQUOTA);
176 if (strchr(valstr, 'p'))
177 enabled |= BIT(PRJQUOTA);
179 if (enabled == 0 && strcmp(valstr, "none"))
180 GOTO(out, count = -EINVAL);
183 pool = LQUOTA_RES_MD;
185 pool = LQUOTA_RES_DT;
187 if (server_name2fsname(qsd->qsd_svname, fsname, NULL))
188 GOTO(out, count = -EINVAL);
190 rc = qsd_config(valstr, fsname, pool);
196 LPROC_SEQ_FOPS(qsd_enabled);
198 /* force reintegration procedure to be executed.
199 * Used for test/debugging purpose */
201 lprocfs_force_reint_seq_write(struct file *file, const char __user *buffer,
202 size_t count, loff_t *off)
204 struct seq_file *m = file->private_data;
205 struct qsd_instance *qsd = m->private;
208 LASSERT(qsd != NULL);
210 write_lock(&qsd->qsd_lock);
211 if (qsd->qsd_stopping) {
212 /* don't mess up with shutdown procedure, it is already
213 * complicated enough */
215 } else if (!qsd->qsd_prepared) {
218 /* mark all indexes as stale */
219 for (qtype = USRQUOTA; qtype < LL_MAXQUOTAS; qtype++) {
220 qsd->qsd_type_array[qtype]->qqi_glb_uptodate = false;
221 qsd->qsd_type_array[qtype]->qqi_slv_uptodate = false;
224 write_unlock(&qsd->qsd_lock);
229 /* kick off reintegration */
230 for (qtype = USRQUOTA; qtype < LL_MAXQUOTAS; qtype++) {
231 rc = qsd_start_reint_thread(qsd->qsd_type_array[qtype]);
235 return rc == 0 ? count : rc;
237 LPROC_SEQ_FOPS_WR_ONLY(qsd, force_reint);
239 static int qsd_timeout_seq_show(struct seq_file *m, void *data)
241 struct qsd_instance *qsd = m->private;
242 LASSERT(qsd != NULL);
244 seq_printf(m, "%d\n", qsd_wait_timeout(qsd));
249 qsd_timeout_seq_write(struct file *file, const char __user *buffer,
250 size_t count, loff_t *off)
252 struct seq_file *m = file->private_data;
253 struct qsd_instance *qsd = m->private;
257 LASSERT(qsd != NULL);
258 rc = kstrtoll_from_user(buffer, count, 0, &timeout);
265 qsd->qsd_timeout = timeout;
268 LPROC_SEQ_FOPS(qsd_timeout);
270 static struct lprocfs_vars lprocfs_quota_qsd_vars[] = {
272 .fops = &qsd_state_fops },
274 .fops = &qsd_enabled_fops },
275 { .name = "force_reint",
276 .fops = &qsd_force_reint_fops },
278 .fops = &qsd_timeout_fops },
283 * Callback function invoked by the OSP layer when the connection to the master
286 * \param data - is a pointer to the qsd_instance
288 * \retval - 0 on success, appropriate error on failure
290 static int qsd_conn_callback(void *data)
292 struct qsd_instance *qsd = (struct qsd_instance *)data;
296 /* qsd_exp should now be valid */
297 LASSERT(qsd->qsd_exp);
299 qsd->qsd_ns = class_exp2obd(qsd->qsd_exp)->obd_namespace;
301 write_lock(&qsd->qsd_lock);
302 /* notify that qsd_exp is now valid */
303 qsd->qsd_exp_valid = true;
304 write_unlock(&qsd->qsd_lock);
306 /* Now that the connection to master is setup, we can initiate the
307 * reintegration procedure for quota types which are enabled.
308 * It is worth noting that, if the qsd_instance hasn't been started
309 * already, then we can only complete the first two steps of the
310 * reintegration procedure (i.e. global lock enqueue and slave
311 * index transfer) since the space usage reconciliation (i.e.
312 * step 3) will have to wait for qsd_start() to be called */
313 for (type = USRQUOTA; type < LL_MAXQUOTAS; type++) {
314 struct qsd_qtype_info *qqi = qsd->qsd_type_array[type];
315 wake_up(&qqi->qqi_reint_thread.t_ctl_waitq);
322 * Release qsd_qtype_info structure which contains data associated with a
323 * given quota type. This releases the accounting objects.
324 * It's called on OSD cleanup when the qsd instance is released.
326 * \param env - is the environment passed by the caller
327 * \param qsd - is the qsd instance managing the qsd_qtype_info structure
329 * \param qtype - is the quota type to be shutdown
331 static void qsd_qtype_fini(const struct lu_env *env, struct qsd_instance *qsd,
334 struct qsd_qtype_info *qqi;
338 if (qsd->qsd_type_array[qtype] == NULL)
340 qqi = qsd->qsd_type_array[qtype];
341 qsd->qsd_type_array[qtype] = NULL;
343 /* all deferred work lists should be empty */
344 LASSERT(list_empty(&qqi->qqi_deferred_glb));
345 LASSERT(list_empty(&qqi->qqi_deferred_slv));
347 /* shutdown lquota site */
348 if (qqi->qqi_site != NULL && !IS_ERR(qqi->qqi_site)) {
349 lquota_site_free(env, qqi->qqi_site);
350 qqi->qqi_site = NULL;
353 /* The qqi may still be holding by global locks which are being
354 * canceled asynchronously (LU-4365), see the following steps:
356 * - On server umount, we try to clear all quota locks first by
357 * disconnecting LWP (which will invalidate import and cleanup
358 * all locks on it), however, if quota reint process is holding
359 * the global lock for reintegration at that time, global lock
360 * will fail to be cleared on LWP disconnection.
362 * - Umount process goes on and stops reint process, the global
363 * lock will be dropped on reint process exit, however, the lock
364 * cancel in done in asynchronous way, so the
365 * qsd_glb_blocking_ast() might haven't been called yet when we
368 while (atomic_read(&qqi->qqi_ref) > 1) {
369 CDEBUG(D_QUOTA, "qqi reference count %u, repeat: %d\n",
370 atomic_read(&qqi->qqi_ref), repeat);
372 set_current_state(TASK_INTERRUPTIBLE);
373 schedule_timeout(cfs_time_seconds(1));
376 /* by now, all qqi users should have gone away */
377 LASSERT(atomic_read(&qqi->qqi_ref) == 1);
378 lu_ref_fini(&qqi->qqi_reference);
380 /* release accounting object */
381 if (qqi->qqi_acct_obj != NULL && !IS_ERR(qqi->qqi_acct_obj)) {
382 dt_object_put(env, qqi->qqi_acct_obj);
383 qqi->qqi_acct_obj = NULL;
386 /* release slv index */
387 if (qqi->qqi_slv_obj != NULL && !IS_ERR(qqi->qqi_slv_obj)) {
388 dt_object_put(env, qqi->qqi_slv_obj);
389 qqi->qqi_slv_obj = NULL;
390 qqi->qqi_slv_ver = 0;
393 /* release global index */
394 if (qqi->qqi_glb_obj != NULL && !IS_ERR(qqi->qqi_glb_obj)) {
395 dt_object_put(env, qqi->qqi_glb_obj);
396 qqi->qqi_glb_obj = NULL;
397 qqi->qqi_glb_ver = 0;
404 static const char *qtype2acct_name(int qtype)
406 static char unknown[24];
414 return "acct_project";
417 snprintf(unknown, sizeof(unknown), "acct_unknown_%u", qtype);
421 static const char *qtype2glb_name(int qtype)
423 static char unknown[24];
429 return "limit_group";
431 return "limit_project";
434 snprintf(unknown, sizeof(unknown), "acct_unknown_%u", qtype);
439 * Allocate and initialize a qsd_qtype_info structure for quota type \qtype.
440 * This opens the accounting object and initializes the proc file.
441 * It's called on OSD start when the qsd_prepare() is invoked on the qsd
444 * \param env - the environment passed by the caller
445 * \param qsd - is the qsd instance which will be in charge of the new
446 * qsd_qtype_info instance.
447 * \param qtype - is quota type to set up
449 * \retval - 0 on success and qsd->qsd_type_array[qtype] is allocated,
450 * appropriate error on failure
452 static int qsd_qtype_init(const struct lu_env *env, struct qsd_instance *qsd,
455 struct qsd_qtype_info *qqi;
457 struct obd_uuid uuid;
460 LASSERT(qsd->qsd_type_array[qtype] == NULL);
462 /* allocate structure for this quota type */
466 qsd->qsd_type_array[qtype] = qqi;
467 atomic_set(&qqi->qqi_ref, 1); /* referenced from qsd */
469 /* set backpointer and other parameters */
471 qqi->qqi_qtype = qtype;
472 lu_ref_init(&qqi->qqi_reference);
473 qqi->qqi_glb_uptodate = false;
474 qqi->qqi_slv_uptodate = false;
475 qqi->qqi_reint = false;
476 init_waitqueue_head(&qqi->qqi_reint_thread.t_ctl_waitq);
477 thread_set_flags(&qqi->qqi_reint_thread, SVC_STOPPED);
478 INIT_LIST_HEAD(&qqi->qqi_deferred_glb);
479 INIT_LIST_HEAD(&qqi->qqi_deferred_slv);
480 lquota_generate_fid(&qqi->qqi_fid, QSD_RES_TYPE(qsd), qtype);
482 /* open accounting object */
483 LASSERT(qqi->qqi_acct_obj == NULL);
484 qqi->qqi_acct_obj = acct_obj_lookup(env, qsd->qsd_dev, qtype);
485 if (IS_ERR(qqi->qqi_acct_obj)) {
486 CDEBUG(D_QUOTA, "%s: no %s space accounting support: rc = %ld\n",
487 qsd->qsd_svname, qtype_name(qtype),
488 PTR_ERR(qqi->qqi_acct_obj));
489 qqi->qqi_acct_obj = NULL;
490 qqi->qqi_acct_failed = true;
493 /* open global index copy */
494 LASSERT(qqi->qqi_glb_obj == NULL);
495 qqi->qqi_glb_obj = lquota_disk_glb_find_create(env, qsd->qsd_dev,
497 &qqi->qqi_fid, true);
498 if (IS_ERR(qqi->qqi_glb_obj)) {
499 CERROR("%s: can't open global index copy "DFID" %ld\n",
500 qsd->qsd_svname, PFID(&qqi->qqi_fid),
501 PTR_ERR(qqi->qqi_glb_obj));
502 GOTO(out, rc = PTR_ERR(qqi->qqi_glb_obj));
504 qqi->qqi_glb_ver = dt_version_get(env, qqi->qqi_glb_obj);
506 /* open slave index copy */
507 LASSERT(qqi->qqi_slv_obj == NULL);
508 obd_str2uuid(&uuid, qsd->qsd_svname);
509 qqi->qqi_slv_obj = lquota_disk_slv_find_create(env, qsd->qsd_dev,
511 &qqi->qqi_fid, &uuid,
513 if (IS_ERR(qqi->qqi_slv_obj)) {
514 CERROR("%s: can't open slave index copy "DFID" %ld\n",
515 qsd->qsd_svname, PFID(&qqi->qqi_fid),
516 PTR_ERR(qqi->qqi_slv_obj));
517 GOTO(out, rc = PTR_ERR(qqi->qqi_slv_obj));
519 qqi->qqi_slv_ver = dt_version_get(env, qqi->qqi_slv_obj);
522 qqi->qqi_site = lquota_site_alloc(env, qqi, false, qtype, &qsd_lqe_ops);
523 if (IS_ERR(qqi->qqi_site)) {
524 CERROR("%s: can't allocate site "DFID" %ld\n", qsd->qsd_svname,
525 PFID(&qqi->qqi_fid), PTR_ERR(qqi->qqi_site));
526 GOTO(out, rc = PTR_ERR(qqi->qqi_site));
529 /* register proc entry for accounting & global index copy objects */
530 rc = lprocfs_seq_create(qsd->qsd_proc, qtype2acct_name(qtype),
531 0444, &lprocfs_quota_seq_fops,
534 CERROR("%s: can't add procfs entry for accounting file %d\n",
535 qsd->qsd_svname, rc);
539 rc = lprocfs_seq_create(qsd->qsd_proc, qtype2glb_name(qtype),
540 0444, &lprocfs_quota_seq_fops,
543 CERROR("%s: can't add procfs entry for global index copy %d\n",
544 qsd->qsd_svname, rc);
550 qsd_qtype_fini(env, qsd, qtype);
555 * Release a qsd_instance. Companion of qsd_init(). This releases all data
556 * structures associated with the quota slave (on-disk objects, lquota entry
558 * This function should be called when the OSD is shutting down.
560 * \param env - is the environment passed by the caller
561 * \param qsd - is the qsd instance to shutdown
563 void qsd_fini(const struct lu_env *env, struct qsd_instance *qsd)
568 if (unlikely(qsd == NULL))
571 CDEBUG(D_QUOTA, "%s: initiating QSD shutdown\n", qsd->qsd_svname);
572 write_lock(&qsd->qsd_lock);
573 qsd->qsd_stopping = true;
574 write_unlock(&qsd->qsd_lock);
576 /* remove qsd proc entry */
577 if (qsd->qsd_proc != NULL) {
578 lprocfs_remove(&qsd->qsd_proc);
579 qsd->qsd_proc = NULL;
582 /* stop the writeback thread */
583 qsd_stop_upd_thread(qsd);
585 /* shutdown the reintegration threads */
586 for (qtype = USRQUOTA; qtype < LL_MAXQUOTAS; qtype++) {
587 if (qsd->qsd_type_array[qtype] == NULL)
589 qsd_stop_reint_thread(qsd->qsd_type_array[qtype]);
592 if (qsd->qsd_ns != NULL) {
596 /* release per-filesystem information */
597 if (qsd->qsd_fsinfo != NULL) {
598 mutex_lock(&qsd->qsd_fsinfo->qfs_mutex);
599 /* remove from the list of fsinfo */
600 list_del_init(&qsd->qsd_link);
601 mutex_unlock(&qsd->qsd_fsinfo->qfs_mutex);
602 qsd_put_fsinfo(qsd->qsd_fsinfo);
603 qsd->qsd_fsinfo = NULL;
606 /* free per-quota type data */
607 for (qtype = USRQUOTA; qtype < LL_MAXQUOTAS; qtype++)
608 qsd_qtype_fini(env, qsd, qtype);
610 /* deregister connection to the quota master */
611 qsd->qsd_exp_valid = false;
612 lustre_deregister_lwp_item(&qsd->qsd_exp);
614 /* release quota root directory */
615 if (qsd->qsd_root != NULL) {
616 dt_object_put(env, qsd->qsd_root);
617 qsd->qsd_root = NULL;
620 /* release reference on dt_device */
621 if (qsd->qsd_dev != NULL) {
622 lu_ref_del(&qsd->qsd_dev->dd_lu_dev.ld_reference, "qsd", qsd);
623 lu_device_put(&qsd->qsd_dev->dd_lu_dev);
627 CDEBUG(D_QUOTA, "%s: QSD shutdown completed\n", qsd->qsd_svname);
631 EXPORT_SYMBOL(qsd_fini);
634 * Create a new qsd_instance to be associated with backend osd device
635 * identified by \dev.
637 * \param env - the environment passed by the caller
638 * \param svname - is the service name of the OSD device creating this instance
639 * \param dev - is the dt_device where to store quota index files
640 * \param osd_proc - is the procfs parent directory where to create procfs file
641 * related to this new qsd instance
643 * \retval - pointer to new qsd_instance associated with dev \dev on success,
644 * appropriate error on failure
646 struct qsd_instance *qsd_init(const struct lu_env *env, char *svname,
647 struct dt_device *dev,
648 struct proc_dir_entry *osd_proc, bool is_md)
650 struct qsd_thread_info *qti = qsd_info(env);
651 struct qsd_instance *qsd;
655 /* only configure qsd for MDT & OST */
656 type = server_name2index(svname, &idx, NULL);
657 if (type != LDD_F_SV_TYPE_MDT && type != LDD_F_SV_TYPE_OST)
660 /* allocate qsd instance */
663 RETURN(ERR_PTR(-ENOMEM));
665 /* generic initializations */
666 rwlock_init(&qsd->qsd_lock);
667 INIT_LIST_HEAD(&qsd->qsd_link);
668 thread_set_flags(&qsd->qsd_upd_thread, SVC_STOPPED);
669 init_waitqueue_head(&qsd->qsd_upd_thread.t_ctl_waitq);
670 INIT_LIST_HEAD(&qsd->qsd_upd_list);
671 spin_lock_init(&qsd->qsd_adjust_lock);
672 INIT_LIST_HEAD(&qsd->qsd_adjust_list);
673 qsd->qsd_prepared = false;
674 qsd->qsd_started = false;
675 qsd->qsd_is_md = is_md;
677 /* copy service name */
678 if (strlcpy(qsd->qsd_svname, svname, sizeof(qsd->qsd_svname))
679 >= sizeof(qsd->qsd_svname))
680 GOTO(out, rc = -E2BIG);
682 /* grab reference on osd device */
683 lu_device_get(&dev->dd_lu_dev);
684 lu_ref_add(&dev->dd_lu_dev.ld_reference, "qsd", qsd);
687 /* get fsname from svname */
688 rc = server_name2fsname(svname, qti->qti_buf, NULL);
690 CERROR("%s: fail to extract filesystem name\n", svname);
694 /* look up quota setting for the filesystem the target belongs to */
695 qsd->qsd_fsinfo = qsd_get_fsinfo(qti->qti_buf, 1);
696 if (qsd->qsd_fsinfo == NULL) {
697 CERROR("%s: failed to locate filesystem information\n", svname);
698 GOTO(out, rc = -EINVAL);
701 /* add in the list of lquota_fsinfo */
702 mutex_lock(&qsd->qsd_fsinfo->qfs_mutex);
703 list_add_tail(&qsd->qsd_link, &qsd->qsd_fsinfo->qfs_qsd_list);
704 mutex_unlock(&qsd->qsd_fsinfo->qfs_mutex);
706 /* register procfs directory */
708 qsd->qsd_proc = lprocfs_register(QSD_DIR_MD, osd_proc,
709 lprocfs_quota_qsd_vars, qsd);
711 qsd->qsd_proc = lprocfs_register(QSD_DIR_DT, osd_proc,
712 lprocfs_quota_qsd_vars, qsd);
714 if (type == LDD_F_SV_TYPE_MDT && qsd->qsd_is_md)
715 lprocfs_add_symlink(QSD_DIR, osd_proc, "./%s", QSD_DIR_MD);
716 else if (type == LDD_F_SV_TYPE_OST && !qsd->qsd_is_md)
717 lprocfs_add_symlink(QSD_DIR, osd_proc, "./%s", QSD_DIR_DT);
719 if (IS_ERR(qsd->qsd_proc)) {
720 rc = PTR_ERR(qsd->qsd_proc);
721 qsd->qsd_proc = NULL;
722 CERROR("%s: fail to create quota slave proc entry (%d)\n",
734 EXPORT_SYMBOL(qsd_init);
737 * Initialize on-disk structures in order to manage quota enforcement for
738 * the target associated with the qsd instance \qsd and starts the reintegration
739 * procedure for each quota type as soon as possible.
740 * The last step of the reintegration will be completed once qsd_start() is
741 * called, at which points the space reconciliation with the master will be
743 * This function must be called when the server stack is fully configured,
744 * typically when ->ldo_prepare is called across the stack.
746 * \param env - the environment passed by the caller
747 * \param qsd - is qsd_instance to prepare
749 * \retval - 0 on success, appropriate error on failure
751 int qsd_prepare(const struct lu_env *env, struct qsd_instance *qsd)
753 struct qsd_thread_info *qti = qsd_info(env);
757 if (unlikely(qsd == NULL))
760 read_lock(&qsd->qsd_lock);
761 if (qsd->qsd_prepared) {
762 CERROR("%s: qsd instance already prepared\n", qsd->qsd_svname);
765 read_unlock(&qsd->qsd_lock);
769 /* Record whether this qsd instance is managing quota enforcement for a
770 * MDT (i.e. inode quota) or OST (block quota) */
772 qsd->qsd_sync_threshold = LQUOTA_LEAST_QUNIT(LQUOTA_RES_MD);
774 qsd->qsd_sync_threshold = LQUOTA_LEAST_QUNIT(LQUOTA_RES_DT);
776 /* look-up on-disk directory for the quota slave */
777 qsd->qsd_root = lquota_disk_dir_find_create(env, qsd->qsd_dev, NULL,
779 if (IS_ERR(qsd->qsd_root)) {
780 rc = PTR_ERR(qsd->qsd_root);
781 qsd->qsd_root = NULL;
782 CERROR("%s: failed to create quota slave root dir (%d)\n",
783 qsd->qsd_svname, rc);
787 /* initialize per-quota type data */
788 for (qtype = USRQUOTA; qtype < LL_MAXQUOTAS; qtype++) {
789 rc = qsd_qtype_init(env, qsd, qtype);
794 /* pools successfully setup, mark the qsd as prepared */
795 write_lock(&qsd->qsd_lock);
796 qsd->qsd_prepared = true;
797 write_unlock(&qsd->qsd_lock);
799 if (qsd->qsd_dev->dd_rdonly)
802 /* start reintegration thread for each type, if required */
803 for (qtype = USRQUOTA; qtype < LL_MAXQUOTAS; qtype++) {
804 struct qsd_qtype_info *qqi = qsd->qsd_type_array[qtype];
806 if (qsd_type_enabled(qsd, qtype) &&
807 qqi->qqi_acct_failed) {
808 LCONSOLE_ERROR("%s: can't enable quota enforcement "
809 "since space accounting isn't functional"
810 ". Please run tunefs.lustre --quota on "
811 "an unmounted filesystem if not done "
812 "already\n", qsd->qsd_svname);
816 rc = qsd_start_reint_thread(qqi);
818 CERROR("%s: failed to start reint thread for type %s: rc = %d\n",
819 qsd->qsd_svname, qtype_name(qtype), rc);
824 /* start writeback thread */
825 rc = qsd_start_upd_thread(qsd);
827 CERROR("%s: failed to start writeback thread (%d)\n",
828 qsd->qsd_svname, rc);
832 /* generate osp name */
833 rc = tgt_name2lwp_name(qsd->qsd_svname, qti->qti_buf,
836 CERROR("%s: failed to generate ospname (%d)\n",
837 qsd->qsd_svname, rc);
841 /* the connection callback will start the reintegration
842 * procedure if quota is enabled */
843 rc = lustre_register_lwp_item(qti->qti_buf, &qsd->qsd_exp,
844 qsd_conn_callback, (void *)qsd);
846 CERROR("%s: fail to get connection to master (%d)\n",
847 qsd->qsd_svname, rc);
853 EXPORT_SYMBOL(qsd_prepare);
856 * Start a qsd instance. This will complete the last step of the reintegration
857 * procedure as soon as possible (provided that the master is reachable).
858 * This should be called when recovery has been completed and quota should now
859 * be enforced on every operations.
861 * \param env - the environment passed by the caller
862 * \param qsd - is the qsd instance associated with the osd device to start
864 int qsd_start(const struct lu_env *env, struct qsd_instance *qsd)
869 if (unlikely(qsd == NULL))
872 write_lock(&qsd->qsd_lock);
873 if (!qsd->qsd_prepared) {
874 CERROR("%s: can't start qsd instance since it wasn't properly "
875 "initialized\n", qsd->qsd_svname);
877 } else if (qsd->qsd_started) {
878 CERROR("%s: qsd instance already started\n", qsd->qsd_svname);
881 /* notify that the qsd_instance is now started */
882 qsd->qsd_started = true;
884 write_unlock(&qsd->qsd_lock);
889 /* Trigger the 3rd step of reintegration: If usage > granted, acquire
890 * up to usage; If usage < granted, release down to usage. */
891 for (type = USRQUOTA; type < LL_MAXQUOTAS; type++) {
892 struct qsd_qtype_info *qqi = qsd->qsd_type_array[type];
893 wake_up(&qqi->qqi_reint_thread.t_ctl_waitq);
898 EXPORT_SYMBOL(qsd_start);
900 void lustre_register_quota_process_config(int (*qpc)(struct lustre_cfg *lcfg));
903 * Global initialization performed at module load time
905 int qsd_glb_init(void)
909 rc = lu_kmem_init(qsd_caches);
913 qsd_key_init_generic(&qsd_thread_key, NULL);
914 lu_context_key_register(&qsd_thread_key);
915 lustre_register_quota_process_config(qsd_process_config);
921 * Companion of qsd_glb_init() called at module unload time
923 void qsd_glb_fini(void)
925 lustre_register_quota_process_config(NULL);
926 lu_kmem_fini(qsd_caches);
927 lu_context_key_degister(&qsd_thread_key);